简体   繁体   中英

Cancelling a notification received in background from GCM (Android)

I'm using Google Cloud Messaging to receive new orders into an app. I'm trying to handle cases where the same order is sent twice. I just want the second receipt to be ignored, unfortuntately when the app is in the background I dont seem to be able to cancel the notification (ie it still makes a noise and sends a message). The app works fine when in the foreground, putting cancel notification code in my GCMBrodacastreceiver doesnt seem to do anything. Am I missing something?

NotificationManager mNotify = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

mNotify.cancelAll();

You can set the "tag" field in the notification payload. If you use this the incoming notification will just update any existing one with the same tag.

cancelAll() will dismiss the notification, but your code may not be invoked when the app is in the background as the notification will be posted without your app's code running. One option would be to have your server not send the notification if it has already sent one recently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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