如何在 onResume() 中取消通知
How to cancel notification inside onResume()
我的应用程序在收到新消息时推送通知。一切正常。当用户单击通知时,它会按预期恢复 Activity
,并取消(删除)通知。
但是,当用户重新打开应用程序时(不是通过通知),通知仍然保留在状态栏中。
所以,我想在Activity
的onResume()
方法里面取消通知。
创建通知时的代码如下:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setContentTitle(channel + ": Unread messages")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.FLAG_AUTO_CANCEL);
notificationManager.notify(0, notification.build());
你可以使用这个:
public void clearNotification() {
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
然后,在 onResume() 方法中调用 clearNotification()
。
在上面的代码中,0
是你的 NOTIFICATION_ID
。
只需调用 notificationManager.cancel(id) 即可将其删除。
当您显示通知时指定它的 ID(当前您设置为 0)- 当您想要取消它时,您需要使用相同的 ID。
NotificationManager.notify()
is an identifier, that you can use to cancel 之后通知中的第一个参数:
private static final int NOTIFICATION_ID = 0;
...
notificationManager.notify(NOTIFICATION_ID, notification.build());
...
notificationMananger.cancel(NOTIFICATION_ID);
我的应用程序在收到新消息时推送通知。一切正常。当用户单击通知时,它会按预期恢复 Activity
,并取消(删除)通知。
但是,当用户重新打开应用程序时(不是通过通知),通知仍然保留在状态栏中。
所以,我想在Activity
的onResume()
方法里面取消通知。
创建通知时的代码如下:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setContentTitle(channel + ": Unread messages")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.FLAG_AUTO_CANCEL);
notificationManager.notify(0, notification.build());
你可以使用这个:
public void clearNotification() {
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
然后,在 onResume() 方法中调用 clearNotification()
。
在上面的代码中,0
是你的 NOTIFICATION_ID
。
只需调用 notificationManager.cancel(id) 即可将其删除。 当您显示通知时指定它的 ID(当前您设置为 0)- 当您想要取消它时,您需要使用相同的 ID。
NotificationManager.notify()
is an identifier, that you can use to cancel 之后通知中的第一个参数:
private static final int NOTIFICATION_ID = 0;
...
notificationManager.notify(NOTIFICATION_ID, notification.build());
...
notificationMananger.cancel(NOTIFICATION_ID);