你需要在创建通知时添加这行代码!
notification.flags |= Notification.FLAG_AUTO_CANCEL;
这将取消点击通知。
进一步阅读: 单击“通知”后打开应用程序
**编辑,添加额外内容以确定是否单击了某个通知 pIntent.putExtra("fromNotification", true);
pIntent.putExtra("fromNotification", true);
您应该在通知中添加标记,然后通过提供正确的ID和正确的标记来清除通知。
如果您传递相同的ID,则不需要通知计数器,因为当通知看到相同的ID时,它会清除旧通知并添加新通知,除非您希望显示该用户收到多个通知。
private static final String TAG = "YourNotification"; private static final int NOTIFICATION_ID = 101; private Notification notification; public NotificationManager notificationManager; //you can create notification with it's own id and tag, text, etc by passing //these variables into the method (int id, String tag, ... etc) public void createNotification() { notification = new Notification.Builder(context) .setContentTitle("Content title") .setContentText("Content text") .setSmallIcon(R.drawable.your_small_icon) .setLargeIcon(bitmapYourLargeIcon) .setContentIntent(pendingIntent) .addAction(R.drawable.icon, pendingIntentAction) .build(); notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(TAG, NOTIFICATION_ID, notification); }
取消通知只需使用此方法:
//clears your notification in 100% cases public void cancelNotification(int id, String tag) { //you can get notificationManager like this: //notificationManage r= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(tag, id); }