问题
Do I need to register for push each time that the app is created? Or is it enough to register once, for example after login? What is the best point to call C2DMessaging.register(getApplicationContext(), C2DMConstants.SENDER); ?
回答1:
No. Its not necessary to register every time when app started. But you have to check whether your registration id is changed by GCM server or not. Because sometimes GCM needs to change registration id and even it does not inform device. So you have to save your registration id in your app and on app start, match the saved id and id returned by GCMRegistrar.getRegistrationId() method. If both are different, save it and also update it on your server.
回答2:
I think you should register the device on GCM each time user logs in and unregister the device whenever user logs out because user may use devices from their friends to access your applciation. So, it would be better to unregister the device each time user logs out. You also need to update your mapping (most probably on your application server) of user and its devices each time user logs in or out.
回答3:
Everytime the app start, check your saved token:
If it is null, send the intent to register for push
If it is not null, you can either check if it has been sent to the server and decide whether to send it.
However, I prefer to send it more often just in case problems happen on the server.
In general, google asked us not to register multiple times. Once you have a token, use it
来源:https://stackoverflow.com/questions/12494842/when-register-for-push