【问题标题】:Android: remove notification from notification barAndroid:从通知栏中删除通知
【发布时间】:2011-04-05 10:41:31
【问题描述】:

我创建了一个应用程序,并通过一个事件设法在 android 通知栏中添加通知。现在我需要示例如何从事件的通知栏中删除该通知?

【问题讨论】:

标签: android notifications


【解决方案1】:

它会帮助你

private var notificationManager = NotificationManagerCompat.from(this)
notificationManager.cancel("your_notification_id")

如果你这样启动通知前台

startForeground("your_notification_id",notification.build())

那么你也应该停止前台服务

notificationManager.cancel("your_notification_id")
stopForeground(true)

【讨论】:

    【解决方案2】:

    一个简短的Liner是:

    NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
    

    或者取消所有通知是:

    NotificationManagerCompat.from(context).cancelAll()
    

    专为 AndroidX 或支持库设计。

    【讨论】:

    • NotificationManagerCompat 和 NotificationManager 有什么区别?如我所见,“compat”是使用 from 静态方法获取的,另一个是使用 getSystemService 调用获取的。
    • 我给出了使用 Androidx 库以实现向后兼容性的简短推荐方法。
    【解决方案3】:

    这会有所帮助:

    NotificationManager mNotificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.cancelAll();
    

    这应该会删除应用发出的所有通知

    如果您通过调用创建通知

    startForeground();
    

    在服务中。您可能需要调用

    stopForeground(false);
    

    先取消通知。

    【讨论】:

    • 如果可能的话,你也能回答这个问题吗?我按照你的建议做了同样的事情,但它不适用于 Android 10。Click here for the question
    【解决方案4】:

    只需调用 ID:

    public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
    

    【讨论】:

      【解决方案5】:

      请试试这个,

      public void removeNotification(Context context, int notificationId) {      
          NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
                  .getSystemService(Context.NOTIFICATION_SERVICE);
          nMgr.cancel(notificationId);
      }
      

      【讨论】:

      • 如果可能的话,你也能回答这个问题吗?我按照你的建议做了同样的事情,但它不适用于 Android 10。Click here for the question
      【解决方案6】:

      NotificationManager.cancel(id) 是正确答案。然而,您可以通过删除整个通知渠道来删除 Android Oreo 和更高版本的通知。这应该会删除已删除频道中的所有消息。

      这是来自Android documentation的示例:

      NotificationManager mNotificationManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      // The id of the channel.
      String id = "my_channel_01";
      mNotificationManager.deleteNotificationChannel(id);
      

      【讨论】:

        【解决方案7】:

        在 Android API >=23 上,您可以执行类似这样的操作来删除一组通知。

          for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
                if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
                    mNotificationManager.cancel(statusBarNotification.getId());
                }
            }
        

        【讨论】:

          【解决方案8】:

          只需像以下代码一样设置 setAutoCancel(True):

          Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);
          
          PendingIntent resultPendingIntent =
                  PendingIntent.getActivity(
                          GameLevelsActivity.this,
                          0,
                          resultIntent,
                          PendingIntent.FLAG_UPDATE_CURRENT
                          );
          
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                  getApplicationContext()).setSmallIcon(R.drawable.icon)
                  .setContentTitle(adv_title)
                  .setContentText(adv_desc)
                  .setContentIntent(resultPendingIntent)
                   //HERE IS WHAT YOY NEED:
                  .setAutoCancel(true);
          
          NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          manager.notify(547, mBuilder.build());`
          

          【讨论】:

          • 这仅在您直接点击通知时才有效。点击操作(如此处:pl.vc/1gvrli)不会删除通知。
          • 这不是被问到的。问题:“我需要示例如何从事件的通知栏中删除该通知??”
          【解决方案9】:

          如果您正在生成通知,该服务在前台使用

          启动
          startForeground(NOTIFICATION_ID, notificationBuilder.build());
          

          然后发行

          notificationManager.cancel(NOTIFICATION_ID);
          

          取消通知不起作用,通知仍然出现在状态栏中。在这种特殊情况下,您将通过 2 种方式解决这些问题:

          1> 在服务中使用 stopForeground( false ):

          stopForeground( false );
          notificationManager.cancel(NOTIFICATION_ID);
          

          2> 使用调用活动销毁该服务类:

          Intent i = new Intent(context, Service.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
          if(ServiceCallingActivity.activity != null) {
              ServiceCallingActivity.activity.finish();
          }
          context.stopService(i);
          

          第二种方式更喜欢音乐播放器通知,因为这种方式不仅可以删除通知,还可以删除播放器...!!

          【讨论】:

          • 感谢您的回答!使用stopForeground(true); manager.cancelAll(); 为我解决了这个问题!
          【解决方案10】:

          您也可以在通知管理器上拨打cancelAll,这样您甚至不必担心通知ID。

          NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
          notifManager.cancelAll();
          

          编辑:我被否决了,所以也许我应该指定这只会从您的应用程序中删除通知。

          【讨论】:

          • 前台没有通知管理器:startForeground(NOTIFICATION_ID, mNotification);
          【解决方案11】:

          这很简单。您必须在 NotificationManager 上调用 cancelcancelAll。 cancel 方法的参数是应该取消的通知的 ID。

          查看 API:http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

          【讨论】:

          • 感谢 Roflcoptr :) 我在这里找到了它:stackoverflow.com/questions/2839727/…
          • 私有静态最终 int MY_NOTIFICATION_ID= 1234;字符串 ns = Context.NOTIFICATION_SERVICE;通知管理器 mNotificationManager; mNotificationManager = (NotificationManager) getSystemService(ns); mNotificationManager.notify(MY_NOTIFICATION_ID, 通知);示例代码不完整。这取决于您创建通知的方式。只需确保使用与创建通知时相同的 id 来取消通知即可。取消:mNotificationManager.cancel(MY_NOTIFICATION_ID);
          • 你拯救了这一天!,很高兴阅读像你这样的人,即使他们不是复杂问题的复杂答案,但像你这样的人在困难时期帮助了我们很多。非常感谢。
          • public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
          • 系统创建的那些通知呢?
          【解决方案12】:

          你可以试试这个快速代码

          public static void cancelNotification(Context ctx, int notifyId) {
              String ns = Context.NOTIFICATION_SERVICE;
              NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
              nMgr.cancel(notifyId);
          }
          

          【讨论】:

          • 这应该是正确的答案,因为它简单、正确并且直截了当。
          • 您好,这是正确的答案。但通知托盘仍然可见。有什么办法可以隐藏吗?例如,我有两个与通知相关的操作:取消和完成。当采取这些操作中的任何一个时,我将删除通知。但是这样做只会删除通知,但通知托盘仍然可见。
          • 1行,直接调用通知id:public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
          • 你不需要 ctx。 NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          【解决方案13】:

          使用 NotificationManager 取消通知。您只需要提供您的通知 ID

          mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

          也检查这个链接 See Developer Link

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-04-24
            • 2013-10-16
            • 2019-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多