Android GCM:通知关闭后重复推送 [英] Android GCM: duplicate push after notification dismiss

查看:390
本文介绍了Android GCM:通知关闭后重复推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 打开启动服务以接收通知的应用程序。 gcm消息与通知有效内容。

  2. 通过向左/向右轻扫此通知来关闭通知。如果它具有 action_click 元素,则点击它也是有效的。

  3. 打开任务并关闭当前应用程序。

  4. 再次打开应用程序。当GCM服务再次启动时,它会收到先前已解除的通知。

我的代码是最基本的代码 GcmListenerService 提供的https://developers.google.com/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:

  1. Open the application that starts the service to receive notifications.
  2. Send gcm message with a notification payload.
  3. Dismiss the notification by swiping left/right this notification. It is also valid to click it if it has an action_click element.
  4. Open tasks and close the current application.
  5. 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:通知关闭后重复推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆