简体   繁体   English

取消在后台从GCM(Android)收到的通知

[英]Cancelling a notification received in background from GCM (Android)

I'm using Google Cloud Messaging to receive new orders into an app. 我正在使用Google Cloud Messaging将新订单接收到应用程序中。 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. 该应用程序在前台运行良好,将取消通知代码放入我的GCMBrodacastreceiver中似乎没有任何作用。 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. cancelAll()将取消该通知,但是当应用程序在后台运行时,可能不会调用您的代码,因为将在不运行应用程序代码的情况下发布通知。 One option would be to have your server not send the notification if it has already sent one recently. 一种选择是让您的服务器如果最​​近已经发送了通知,则不发送通知。

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

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