简体   繁体   English

如何在Android中更新和删除待处理的意图和通知

[英]How to update and delete a Pending Intent and a Notification in android

whenever i attempt to update the notification or Intent a new one is created instead which i don't want. 每当我尝试更新通知或Intent时,都会创建一个我不希望的新通知。 I think the problem is that the Intents are not being identified whenever i'm trying to update it. 我认为问题在于,每当我尝试更新Intent时都无法识别它们。 The link to the code is below: 该代码的链接如下:

https://gist.github.com/Kerron-Hutton/03799ac497128919225bbbd4efaffa25 https://gist.github.com/Kerron-Hutton/03799ac497128919225bbbd4efaffa25

  1. To update a PendingIntent , you should use the same REQUEST_CODE that you used before to create it. 要更新PendingIntent ,您应该使用与创建前相同的REQUEST_CODE

     PendingIntent pIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
  2. To cancel/delete an existing PendingIntent , you can use PendingIntent.cancel() method. 要取消/删除现有的PendingIntent ,可以使用PendingIntent.cancel()方法。

     PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel(); 
  3. To update a Notification , you can use NotificationManager.notify() method with the NOTIFICATION_ID that you want to update. 要更新Notification ,可以将NotificationManager.notify()方法与要更新的NOTIFICATION_ID一起使用。

     NotificationManager notificationManager= (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); 

    Notification id should be unique within your application. 通知id在您的应用程序中应该是唯一的。 If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information. 如果您的应用程序已经发布了具有相同id的通知,但尚未取消,则该通知将由更新的信息代替。

  4. To cancel/remove an existing Notification , use NotificationManager.Cancel() method with the NOTIFICATION_ID that you want to cancel. 要取消/删除现有Notification ,请使用带有要取消的NOTIFICATION_ID NotificationManager.Cancel()方法。

     notificationManager.cancel(NOTIFICATION_ID); 

Hope this will help~ 希望这会有所帮助〜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM