Android C2DM : Duplicate message to the same device and App
Asked Answered
C

5

9

I'm wondering if anyone has faced this issue with Google C2DM? This is the scenario I am faced with:

  1. User installs the app and registers with C2DM server for a registration key.
  2. User uninstalls the app.
  3. User reinstalls the app (and registers with C2DM server for new registration key).

Now I send message from my server to the user's phone and they get a duplicate message.

Could anyone shed any insight into wether this is expected behaviour or how I can fix it? Thanks,

Copolymerize answered 6/7, 2011 at 11:56 Comment(1)
Do you mean that they get the same message twice in quick succession ? That is definitely not the expected behaviour - their cloud key should be marked as no longer used when they uninstall. Silly question and apologies in asking - but are you sure that your server code is duping up their cloud ids? Do you keep a log of all your c2dm server requests and the responses that google sends back ?Faretheewell
M
7

Not sure if this is the best approach, but there's a relevant thread over at the android-c2dm group, where the poster offers one technique:

I am sending registration id in the message, so I can check it against the stored registration id on the device.

If it's not the same, discard it and notify the service that registration Id is no longer in use

Downside is sending registration Id takes up some space in already limited message size. But works perfectly in my case since my original message is no more than a few chars long.

Mahla answered 16/8, 2011 at 0:42 Comment(1)
Great technique! It seems very smartBukharin
S
4

This should only happen for the first push notification after re-installing your application.

Google C2DM service is working in passive mode when it comes to detecting uninstalled applications.

First push notification after uninstalling your application (without unregistering from C2DM!!!) will NOT return any error in response. However, the second push notification will return an "invalid registration" or "not registered" error codes where you can realize the application was uninstalled.

The reason is that C2DM servers return the response code immediately and only then tries to push the client. When client respond that an application was uninstalled, it is deleted from C2DM servers. Next push attempt will return an error code immediately.

Spectre answered 1/3, 2012 at 16:9 Comment(2)
But what happens if user install the app immediately after uninstalling and register for Reg_Id. If the device gets a push using the reg_id that was valid before uninstalling. Will the message go to the device or will it return "invalid registration".Atahualpa
Registration ids are unique per device, application and logged-in Google user. I guess, in the scenario you have described, application would get the same registration id as with the previous installation, so you should be OK.Spectre
W
1

Another solution could be to provide your server with a unique identifier for the device. In that case you can just update the registrationID for that UUID when the device tries to register after re-installation.

Wariness answered 24/11, 2011 at 8:46 Comment(1)
+1 am doing the same implementation in the architecture of my applications. But also if the C2DM service returns that a registration is invalid you can remove it again.Ouzo
P
0

Yup, I've run into the same issue and in my opinion it's a big oversight in the Android C2DM implementation. iOS handles this much better in that an app can only ever receive notifications for one and only one device token (equivalent of the c2dm registration id)

The workaround I use is to send the last 10 characters of the registration id as part of the c2dm payload and then in my onMessage method I do the following check:

    if (!regId.endsWith(bundle.getString("regsuffix"))) return null;
Privity answered 16/10, 2011 at 23:22 Comment(0)
O
0

Both @Zamel and @johan answers are good and need to be combined. If you combine both solutions than you will minimize your server's database.

So the best solution will be to:

  1. Send device id when sending the push token to the server

  2. Update push token when is sent for existing device id

  3. Invalidate push token in the server's database, if push notification returns an "invalid registration" or "not registered" error codes to the server

When push token is recognized as "invalid registration" or "not registered" you can invalidate it(mark it as null), delete the row in the database or implement expiration functionality. It depends on your needs

Ouzo answered 5/5, 2015 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.