Android GCM:通知关闭后重复推送

编程入门 行业动态 更新时间:2024-10-25 22:37:22
本文介绍了Android GCM:通知关闭后重复推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用GCM向Android设备发送了通知,并且它正常到达,但有一种情况是通知在被解雇后显示。重现步骤:

  • 打开启动服务以接收通知的应用程序。 gcm消息与通知有效内容。
  • 通过向左/向右轻扫此通知来关闭通知。如果它具有 action_click 元素,则点击它也是有效的。
  • 打开任务并关闭当前应用程序。
  • 再次打开应用程序。当GCM服务再次启动时,它会收到先前已解除的通知。
  • 我的代码是最基本的代码 GcmListenerService 提供的developers.google/cloud-messaging/android/client =nofollow>示例。只需设置权限,将服务放在清单中,并让Google为您显示通知。在提供的场景发生时,我在服务中的 onMessageReceive 上调用了两次相同的数据。

    是我发送给GCM的通知有效载荷:

    { collapseKey:'push', delayWhileIdle :true, timeToLive:3600, data:undefined,通知:{ body:'test message 2 updated', title:'Notification', icon:'myicon'}, contentAvailable:false }

    编辑:

    • 我使用 play-services- gcm:7.8.0 版本,但也使用7.5进行测试
    • 我可以重复此过程5次,之后,通知将永久解除。 >

    解决方案

    我终于解决了这个问题。使用新的GCM实现,不需要启动带来信息的服务,Google是在需要时启动它,具体取决于推送的选项。

    因此,如果您的代码中包含以下内容,请删除:

    // This服务扩展GcmListenerService Intent intent = new Intent(GCMNotificationService.class,context); context.startService(intent);

    您将停止缓存通知问题,接收两次相同的通知和一些奇怪的其他行为。

    I am sending a notification with GCM to an Android device and it is arriving correctly, but there is one case when the notifications are displaying after being dismissed. The steps to reproduce:

  • Open the application that starts the service to receive notifications.
  • Send gcm message with a notification payload.
  • Dismiss the notification by swiping left/right this notification. It is also valid to click it if it has an action_click element.
  • Open tasks and close the current application.
  • Open the application again. When the GCM service starts again, it receives the previously dismissed notification.
  • My code is the most basic one from the example provided by google using the GcmListenerService. Just set the permissions, put the services in the manifest and let google display the notification for you. I am being called twice in the onMessageReceive on the service with the same data when the scenario provided happened.

    This is the notification payload I am sending to GCM:

    { collapseKey: 'push', delayWhileIdle: true, timeToLive: 3600, data: undefined, notification: { body: 'test message 2 updated', title: 'Notification', icon: 'myicon' }, contentAvailable: false }

    EDIT:

    • I am using the play-services-gcm:7.8.0 version, but also tested with 7.5
    • I can repeat the process 5 times, after that, the notifications gets dismissed forever.

    解决方案

    I finally struggled the problem. With the new GCM implementation, there is no need to start the service that brings the information, is Google who starts it just when it is needed depending on the options of the push.

    So if you have in your code something this lines, remove them:

    //This service extends GcmListenerService Intent intent = new Intent(GCMNotificationService.class, context); context.startService(intent);

    You will stop having problems with cached notifications, receiving two times the same notification and some weird other behaviour.

    更多推荐

    Android GCM:通知关闭后重复推送

    本文发布于:2023-11-27 01:54:06,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:通知   Android   GCM

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!