Android C2DM : Duplicate message to the same device and App

后端 未结 5 453
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-17 21:25

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
相关标签:
5条回答
  • 2021-01-17 21:29

    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;
    
    0 讨论(0)
  • 2021-01-17 21:35

    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.

    0 讨论(0)
  • 2021-01-17 21:38

    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

    0 讨论(0)
  • 2021-01-17 21:49

    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.

    0 讨论(0)
  • 2021-01-17 21:50

    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.

    0 讨论(0)
提交回复
热议问题