Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting Google Cloud Messaging (GCM) registration id everytime application starts

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?

like image 526
chizcracker Avatar asked Nov 09 '12 00:11

chizcracker


People also ask

What is GCM registration ID?

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.

Is GCM deprecated?

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.

What is difference between GCM and FCM?

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.

When was GCM deprecated?

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.


1 Answers

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!

like image 76
Nicholas Avatar answered Nov 14 '22 22:11

Nicholas