我使用GCM向Android设备发送了通知,并且它正常到达,但有一种情况是通知在被解雇后显示。重现步骤:
我的代码是最基本的代码 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:
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:通知关闭后重复推送
发布评论