0

I am experiencing a problem with getting duplicate notifications using GCM push notifications on the Android client side.

I install the app and register with GCM like so: regid = gcm.register(SENDER_ID);

This gives me a registration ID (for example: "ABC"). If I try and re-register I get the same ID ("ABC"). If I clear the app data (in Androids app settings) and reload the app I get the same ID ("ABC"), however if I uninstall the app and reinstall, I get a new ID (for example: "DEF").

This is fine, however after registering the second time, my app will now receive notifications sent to both "ABC" and "DEF". This results in some in my app.

Is there a way to ignore all messages sent to any reg ID except the last one that was registered (in my example: "DEF")?

The example I am using to test is the official GCM-Client (https://developer.android.com/google/gcm/client.html).

LondonAppDev
  • 8,501
  • 8
  • 60
  • 87
  • Where is "ABC" and "DEF" stored? Why don't you just overwrite when you receive a new Registration Id? – dannyroa Dec 18 '14 at 21:58
  • How long do you wait after uninstalling then reinstalling again. the docs say that when the user uninstall the app they get unregistered automatically but it can take a little while for the changes to take place. http://developer.android.com/google/gcm/adv.html#unreg – tyczj Dec 18 '14 at 21:59
  • @dannyroa "ABC" and "DEF" are stored on the server. There is no way to overwrite after the app is uninstalled. – LondonAppDev Dec 18 '14 at 22:13
  • @tyczj that's a good point. I will test again but this time wait for a bit to see if it becomes unregistered on Googles side. – LondonAppDev Dec 18 '14 at 22:14
  • @MarkWinterbottom: You probably need to have device id to know if there's a new Registration Id for the same device and overwrite the old one. See http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id. – dannyroa Dec 18 '14 at 22:17
  • @Mark Winterbottom check [my answer](http://stackoverflow.com/questions/26826869/regarding-canonical-ids-in-gcm-google-cloud-messaging/26827470#26827470) please. Your push server should correct reacts on "cannonical_ids" response from GCM server and immediately replace old reg_id with new one. Hope it helps. – Samik Dec 20 '14 at 21:17
  • Thanks @Samik. I am aware I can do it by updating the key on the server, but the problem is: a) if the user logs out and doesn't have an internet connection, there is no way to update. b) if the user uninstalls the app and re-installs there is no way to update the server (perhaps I could use the phones UUID or something when it's re-installed...). I am switching away from Urban Airship push notifications and didn't have this problem until moving to native GCM. – LondonAppDev Dec 21 '14 at 22:27

1 Answers1

0

There's no way to ignore the messages sent to the old reg id.

The best you can do:

  1. In the client side, try to store some app data on the external storage. This would allow you to identify if the app was previously installed on the device, and to tell your server to replace the old reg id with the new one.

  2. In the server side, handle canonical reg id responses from GCM. This won't prevent the sending of a message to an old reg id, but once you send a message to such reg id, you'll immediately remove the old reg id from your DB, and won't send any more messages to it.

Eran
  • 387,369
  • 54
  • 702
  • 768