how can we get device token for android for push notification?

Tarique Shamsi picture Tarique Shamsi · Nov 6, 2015 · Viewed 58.1k times · Source

i want to implement push notification in my application.i want to know,how we get the device token for push notification in android after enabling the google cloud messaging API.How can we get a device token for a particular device??

Answer

Nikunj picture Nikunj · Nov 6, 2015
try {
     InstanceID instanceID = InstanceID.getInstance(this);

     String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            Log.i(TAG, "GCM Registration Token: " + token);

}catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
}