【问题标题】:NotificationManager.cancel(id) is not working inside a broadcast receiverNotificationManager.cancel(id) 在广播接收器中不起作用
【发布时间】:2012-10-25 06:26:59
【问题描述】:

Android:我试图在安装软件包后取消通知栏中的通知。 我正在做的事情如下:

public class MyBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "MyBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            Uri data = intent.getData();
            //some code goes here
            //get the id of the notification to cancel in some way
            notificationhelper._completeNotificationManager.cancel(id);     
        }
    }
}

在哪里

public class notificationhelper {
    public static NotificationManager _completeNotificationManager = null;

    public void complete() {        
        if (_completeNotificationManager == null)
            _completeNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
            
        Notification notification = new Notification(
            R.drawable.notification,
            _context.getString(R.string.notification),
            System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        _completeNotificationManager.notify(TEXT, id, notification);
    }
}

但是notificationhelper._completeNotificationManager.cancel(id) 不起作用。我尝试使用 notificationhelper._completeNotificationManager.cancelAll(); 并且它有效。我做错了什么?

【问题讨论】:

    标签: android broadcastreceiver notificationmanager


    【解决方案1】:

    根据我的经验,无论标签如何,您都无法取消具有特定 ID 的所有通知。

    也就是说,如果您像这样创建两个通知:

    notificationManager.notify(TAG_ONE, SAME_ID, notification_one);
    notificationManager.notify(TAG_TWO, SAME_ID, notification_two);
    

    那么,notificationManager.cancel(SAME_ID) 不会取消其中任何一个!我怀疑这是因为“tag”字段,如果在 notify() 和 cancel() 中未指定,则默认为 null,您必须明确取消。

    所以,要取消这两个通知,你必须调用:

    notificationManager.cancel(TAG_ONE, SAME_ID);
    notificationManager.cancel(TAG_TWO, SAME_ID);
    

    在您的情况下,您提供“TEXT”作为标签,但仅使用 id 取消,默认使用 tag=null。

    所以,要么不要提供 TEXT 作为你的标签:

    _completeNotificationManager.notify(id, notification);
    

    或者,如果您需要单独的通知并且不希望它们相互干扰,请跟踪活动标签:

    _completeNotificationManager.notify(TEXT, id, notification);
    collectionOfActiveTags.add(TEXT);
    
    ...
    
    for (String activeTag : collectionOfActiveTags)    
        notificationhelper._completeNotificationManager.cancel(activeTag, id);
    

    我希望您尝试做的事情得到支持,因为它似乎应该是。

    【讨论】:

    • ("你不能取消所有具有特定 ID 的通知") 注意......你可以,因为需要为每个通知分配一个带有 id unic 的 "tag unic to all notif"。
    【解决方案2】:

    好吧,这在这一点上可能无关紧要,但应该在这里发布,以便像我这样处理相同问题的人可以找到解决方案。

    如果NotificationManager.cancel() 不起作用,请尝试更改通知的 ID。

    notificationManager.notify(NOTIFICATION_ID, notification);
    

    当我将 NOTIFICATION_ID 从 1 更改为 [RANDOM_NUMBER] 时,它神奇地开始工作。我假设 1 以某种方式被保留,尽管在任何文档中都没有注释...

    当然要确保您使用相同的 NOTIFICATION_ID 来取消:

    notificationManager.cancel(NOTIFICATION_ID);
    

    【讨论】:

    • 我对同样的问题摸不着头脑。 stackoverflow.com/questions/38880330 拯救了我的一天。
    • 我会生成一个 7 位的随机数,但仍然通知 concel 不起作用:/
    • 只是一个评论:如果你使用一个随机数,在某些时候你会有一个重复的通知 id 并且 cancel() 方法会再次失败。通知最好使用唯一的 id。
    【解决方案3】:

    我的通知没有被删除,因为我的服务是前台服务,而不是 StartService 启动的常规服务。

    如果您的服务在前台,请调用 stopForeground(true) 而不是 stopself()。所以现在我的代码如下所示:

    NotificationManagerCompat.from(this).cancel(NotificationHelper.PLAYER_NOTIFICATION_ID);
    stopForeground(true);
    

    它成功了,通知被删除了。

    【讨论】:

    • 但是不停止前台服务会取消它的通知吗?
    • 是的,这就是为什么我们调用 stopForeground(true) 来停止前台服务。
    【解决方案4】:

    我最近遇到了同样的问题。我已经解决了。

    据我了解。

    1. 使用基本上是随机数的 id 来通知并将这个相同的 id 发送到要取消它的代码段(接收器/活动...)。

    2. 使用标签时,它似乎对我不起作用,因为我给所有通知一个标签,但具有唯一的 ID。它只适用于第一个标签,所以我完全避免使用标签。如果要使用标签,请发出唯一标签和唯一 ID,并在取消时同时使用它们。

    所以最后的答案......我使用了什么,什么对我有用:

    第 1 步:

    int notif_id = (int)(System.currentTimeMillis()%10000);
    

    第 2 步:在操作意图中添加此 id(我正在启动一个活动,其中通知在操作点击时被取消):

    Intent notificationSettingsIntent = new Intent(context.getApplicationContext(), NotificationSettingsActivity.class);
    notificationSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationSettingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    notificationSettingsIntent.putExtra("fromNotification",true);
    notificationSettingsIntent.putExtra("notif_id",notif_id);
    PendingIntent notificationSettingsActivityPendingIntent = PendingIntent.getActivity(context,notif_id,notificationSettingsIntent,PendingIntent.FLAG_ONE_SHOT);
    

    第 3 步:使用第 1 步中的 id 通知,但没有标签

    NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());
    
    notificationCompat.notify(notif_id,notificationBuilder.build());
    

    现在在通过我的操作点击打开的活动中,我取消通知:

    NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext());
    
    notificationCompat.cancel(getIntent().getIntExtra("notif_id"));
    

    现在每次都有效。

    【讨论】:

      【解决方案5】:

      抱歉迟到了! 但以下对我来说效果很好。

      NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
      mNotificationManager.cancel("<TAG>",<Notificatoin-id>);
      

      【讨论】:

        【解决方案6】:

        以下对我有用:

        final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context.getApplicationContext());
        mNotificationManager.cancel(<Notificatoin-id>);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-14
          • 2021-12-13
          • 2017-10-12
          相关资源
          最近更新 更多