首页 > 解决方案 > 取消后振动不停止

问题描述

我正在尝试设置一个持续 30 秒并在显示时振动的抬头通知。但是,当取消或让通知超时时,振动不会停止某些设备上的振动。

它不适用于运行 Android 9 的 Pixel 3。它适用于运行 Android 9 的 OnePlus 6 或运行 Android 10 Beta 的 Pixel。

我尝试在启动和取消尝试时使用相同的上下文来生成 NotificationManagerCompat。我尝试取消与我的应用程序相关的所有通知,而不仅仅是我真正想要取消的通知。我尝试启动广播并启动接收器,从而取消通知。

这是我正在使用的通知渠道:

AudioAttributes audioAttr =
            new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build();

NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
      if (notificationManager != null) {
        NotificationChannel channel = new NotificationChannel("channelId",
            "channelName",
            NotificationManager.IMPORTANCE_HIGH);
     channel.setDescription("Channel description");
        channel.setVibrationPattern({0L, 2000L, 2000L});
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), audioAttr);
        channel.enableVibration(true);
        channel.setLockscreenVisibility(VISIBILITY_PUBLIC);
        channel.setShowBadge(false);
        notificationManager.createNotificationChannel(ch);

这是启动的通知:


      Notification incCall = new NotificationCompat.Builder(context,
          "someChannelId").setSmallIcon(R.drawable.someDrawable)
          .setContentText("Some text")
          .setContentTitle("Some title")
          .setPriority(NotificationCompat.PRIORITY_HIGH)
          .setCategory(NotificationCompat.CATEGORY_CALL)
          .setDefaults(Notification.DEFAULT_ALL)
          .setAutoCancel(true)
          .setFullScreenIntent(intent, true)
          .setTimeoutAfter(30 * 1000)
          .build();

      incCall.flags = Notification.FLAG_INSISTENT;

 NotificationManagerCompat.from(context)
          .notify(”some id”, incCall);

这就是我尝试取消通知的方式:


      NotificationManagerCompat.from(context).cancel("someChannelId");

我也试过:

      NotificationManagerCompat.from(context).cancelAll();

我希望隐藏通知时手机应该停止振动。但是,即使在调用取消时隐藏通知,振动也会继续。

标签: androidnotifications

解决方案


查看谷歌自己编写的代码,频道和通知的配置有所不同:

http://androidxref.com/8.1.0_r33/xref/packages/services/Telecomm/src/com/android/server/telecom/ui/IncomingCallNotifier.java

删除线:

incCall.flags = Notification.FLAG_INSISTENT

隐藏通知时导致振动停止。然而,它确实使它停止在其他型号上工作,例如运行 Android 10 Beta 的 Pixel。因此,我完全适应了链接中使用的解决方案。通知的构建如下:

 Notification incCall = new NotificationCompat.Builder(context,
      "id")
      .setPriority(NotificationCompat.PRIORITY_MAX)
      .setCategory(NotificationCompat.CATEGORY_CALL)
      .setSmallIcon(R.drawable.someDrawable)
      .setContentTitle("title")
      .setContentText("content")
      .setAutoCancel(true)
      .setOngoing(true)
      .setTimeoutAfter(30*1000)
      .setFullScreenIntent(intent, true)
      .build();

该通知通道的构建如下:

    NotificationChannel channel = new NotificationChannel("id",
        "name",
        NotificationManager.IMPORTANCE_HIGH);
    channel.setDescription("Description");
    AudioAttributes audioAttributes =
        new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build();
    channel.setSound(aRingtoneUri, audioAttributes);
    channel.setLockscreenVisibility(VISIBILITY_PUBLIC);
    channel.setShowBadge(false);
    channel.enableVibration(true);
    notificationManager.createNotificationChannel(channel);

这为我解决了这个问题。


推荐阅读