Get device token or registration id for push notifications in Android app

Here is a solution to find Registration Id in Android app which needs to be given to GCM server:

1. Configure the GCM Intent service properly, it should be something like this

<receiver android:name=“com.google.android.gcm.GCMBroadcastReceiver”android:permission=“com.google.android.c2dm.permission.SEND” > <intent-filter> <actionandroid:name=“com.google.android.c2dm.intent.RECEIVE” /> <actionandroid:name=“com.google.android.c2dm.intent.REGISTRATION” /> <categoryandroid:name=“package_name” /> </intent-filter> </receiver>

2. Then implement the following snippet which will tell you if RegistrationId is already stored in sharedPreference or not.

// Check device for Play Services APK. If check succeeds, proceed with
//  GCM registration.
if (checkPlayServices()) {
gcm
= GoogleCloudMessaging.getInstance(this);
regid
= getRegistrationId(context);

if (regid.isEmpty()) {
registerInBackground
();
}
} else {
Log.i(TAG, “No valid Google Play Services APK found.”);
}

3. Add this snippet to register for GCM service in the background, if there is no registration id stored in shared preferences.

private void registerInBackground() {
    new AsyncTask<void, void,="" string="">() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;

                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.
                // The request to your server should be authenticated if your app
                // is using accounts.
                sendRegistrationIdToBackend();

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the
                // message using the 'from' address in the message.

                // Persist the registration ID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);
    ...
}

Here is how you can get device token or registration id which will be used for push notification.

Thanks for dropping by !! Feel free to comment to this post or you can also drop me an email at naik899@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *