I have read articles about GCM may refresh registration id with no regular cycle. I am trying to build an app using push notification but not so sure how to handle such refreshed registration ids.
My first strategy is requesting registration id everytime the app starts and send it to the app server. It looks working but sounds wrong somehow...
Is it ok to do like this?
A Registration ID is an identifier assigned by GCM to a single instance of a single application installed on an Android device. The device is assigned this identifier when it registers to Google Cloud Messaging. The GCM documentation doesn't specify what information is encoded in this identifier.
The GCM server and client APIs will be removed on May 29, 2019. GCM will be replaced by Firebase Cloud Messaging (FCM), which will inherit the reliable and scalable GCM infrastructure, plus many new features.
FCM is the new version of GCM under the Firebase brand. It inherits GCM's core infrastructure to make sure we continue to deliver messages reliably on Android, iOS and Chrome. Save this answer.
The GCM server and client APIs were removed on May 29, 2019, and currently any calls to those APIs can be expected to fail. Google Cloud Messaging, deprecated April 10 2018, has been deactivated and removed from Google's APIs.
Basically, you should do the following in your main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, GCMIntentService.GCM_SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
}
Afterwards you should send the registration id to your app server, whenever the app receives an com.google.android.c2dm.intent.REGISTRATION
intent with a registration_id
extra. This could happen when Google periodically updates the app's id.
You can achieve this by extending com.google.android.gcm.GCMBaseIntentService
with your own implementation, e.g.:
public class GCMIntentService extends GCMBaseIntentService {
// Also known as the "project id".
public static final String GCM_SENDER_ID = "XXXXXXXXXXXXX";
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(GCM_SENDER_ID);
}
@Override
protected void onRegistered(Context context, String regId) {
// Send the regId to your server.
}
@Override
protected void onUnregistered(Context context, String regId) {
// Unregister the regId at your server.
}
@Override
protected void onMessage(Context context, Intent msg) {
// Handle the message.
}
@Override
protected void onError(Context context, String errorId) {
// Handle the error.
}
}
For more details, I would (re)read the documentation for writing the client side code and the Advanced Section of the GCM documentation.
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With