0

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.
droidpl
  • 5,872
  • 4
  • 35
  • 47
  • I am sending it using nodejs so I think it will not solve your problem on php. Isn't it? – droidpl Aug 21 '15 at 10:17
  • By default each notification pushed from gcm has a Message Id, You can track this Message Id and save in your shared preferences. When you receive another push from GCM just compare the message id and do not display this notification. – Jitender Dev Aug 21 '15 at 10:27
  • Then you mean this behaviour is the real one intended by google? Looks like pretty weird to me and this solution as a workaround. After some random time looks like this stops happening, so maybe there is a way to mark the notification as consumed. – droidpl Aug 21 '15 at 10:29
  • I don't think it's something like what @JitenderDev suggested. I have never come across this kind of a situation unless some error occurred on the server side and it sent the same push notification twice. Can you confirm the push notification is sent from your server only once? – JanithaR Aug 21 '15 at 10:41
  • @JanithaR Even i did not observe this ever. This is still a workaround of the current problem. – Jitender Dev Aug 21 '15 at 10:44
  • Yeah, I can confirm that. Looks like it is something related to caching because if, before opening the application the second time, I disconnect the device from internet, the notification remains coming to the device, which is also awkward. – droidpl Aug 21 '15 at 10:44
  • Well a quick search gives these http://stackoverflow.com/questions/15102055/gcm-duplicated-messages https://groups.google.com/forum/#!topic/android-gcm/EHZUTEJMeyw Seems like something wrong on the Google's side. – JanithaR Aug 21 '15 at 10:47
  • I saw them too but it is from '13, I wish it is not the same error. – droidpl Aug 21 '15 at 10:49
  • Well, ya it's old but you never know. Just wait and see. This suggestion is however 100% based on the assumption that everything from your server and client side working as said by Google. :) – JanithaR Aug 21 '15 at 10:52
  • Well, at least the whole process looks well but this minor thing when the user kills the application. I am trying to downgrade the play services since for me looks more like a client bug. – droidpl Aug 21 '15 at 10:57
  • Also think it could be related to the optional parameters `delayWhileIdle` and `timeToLive` – droidpl Aug 21 '15 at 10:58
  • Ya better. Refer the [GCM Connection Server Reference](https://developers.google.com/cloud-messaging/server-ref) and stick to the defaults and see. – JanithaR Aug 21 '15 at 11:01
  • if any one know the answer for this question please post it.http://stackoverflow.com/questions/32109239/how-to-send-notification-to-android-from-php – scott Aug 21 '15 at 11:06
  • Stick to default, just the notification payload and looks like the behaviour is the same. I am getting mad xD – droidpl Aug 21 '15 at 11:10

1 Answers1

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.

droidpl
  • 5,872
  • 4
  • 35
  • 47