b.i*_*b.i 10 android broadcastreceiver notificationmanager
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪里
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是notificationhelper._completeNotificationManager.cancel(id)不起作用.我尝试使用notificationhelper._completeNotificationManager.cancelAll();它,它的工作原理.我做错了什么?
spi*_*tor 21
根据我的经验,无论标签如何,您都无法取消具有特定ID的所有通知.
也就是说,如果您创建两个通知,如下所示:
notificationManager.notify(TAG_ONE, SAME_ID, notification_one);
notificationManager.notify(TAG_TWO, SAME_ID, notification_two);
Run Code Online (Sandbox Code Playgroud)
然后,notificationManager.cancel(SAME_ID)不会取消其中任何一个!我怀疑这是因为"tag"字段,如果在notify()和cancel()中未指定,则默认为null,您必须明确取消.
因此,要取消这两个通知,您必须致电:
notificationManager.cancel(TAG_ONE, SAME_ID);
notificationManager.cancel(TAG_TWO, SAME_ID);
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您提供"TEXT"作为标记,但仅使用id取消,默认使用tag = null.
因此,要么不提供TEXT作为您的标记:
_completeNotificationManager.notify(id, notification);
Run Code Online (Sandbox Code Playgroud)
或者,如果您需要单独的通知并且不希望它们互相破坏,请跟踪活动标记:
_completeNotificationManager.notify(TEXT, id, notification);
collectionOfActiveTags.add(TEXT);
...
for (String activeTag : collectionOfActiveTags)
notificationhelper._completeNotificationManager.cancel(activeTag, id);
Run Code Online (Sandbox Code Playgroud)
我希望你正在尝试做的事情得到支持,因为它似乎应该是这样.
小智 10
那么这在这一点上可能是无关紧要的,但它应该在这里发布,以便像我一样处理相同问题的人可能会找到解决方案.
如果NotificationManager.cancel()不起作用,请尝试更改通知的ID.
notificationManager.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)
当我NOTIFICATION_ID从1变为[RANDOM_NUMBER]时,它神奇地开始工作.我假设1是以某种方式保留的,尽管在任何文档中都没有注释......
当然要确保使用相同的NOTIFICATION_ID取消:
notificationManager.cancel(NOTIFICATION_ID);
Run Code Online (Sandbox Code Playgroud)
我的通知没有被删除,因为我的服务是Foreground Service,而不是由StartService启动的常规服务。
如果您的服务是前台,请调用stopForeground(true)而不是stopself()。所以现在我的代码看起来像这样:
NotificationManagerCompat.from(this).cancel(NotificationHelper.PLAYER_NOTIFICATION_ID);
stopForeground(true);
Run Code Online (Sandbox Code Playgroud)
并且有效,通知已删除。
| 归档时间: |
|
| 查看次数: |
15707 次 |
| 最近记录: |