Android C2DM : Duplicate message to the same device and App

我的梦境 提交于 2019-12-04 02:59:47

问题


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,


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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;



回答5:


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



来源:https://stackoverflow.com/questions/6595946/android-c2dm-duplicate-message-to-the-same-device-and-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!