Android GCM: duplicate push after notification dis

2020-05-07 05:44发布

问题:

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.

回答1:

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.