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

61,362
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);
}
Share:
61,362

Related videos on Youtube

Tarique Shamsi
Author by

Tarique Shamsi

Salesforce Developer at Tech Mahindra

Updated on July 09, 2022

Comments

  • Tarique Shamsi
    Tarique Shamsi almost 2 years

    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??

    • Rajesh
      Rajesh over 8 years
    • Tarique Shamsi
      Tarique Shamsi over 8 years
      Rajesh,these documentation provided by you based on about registration token id.actually i want to know about particular device token id for push notification
  • Tarique Shamsi
    Tarique Shamsi over 8 years
    in which part of project,we add these codes,and how we get the device token?
  • Nikunj
    Nikunj over 8 years
    you can add above code in any Activity or Service. It's better to get token in Launcher Activity.And put your "SenderId" in R.string.gcm_defaultSenderId
  • Tarique Shamsi
    Tarique Shamsi over 8 years
    but this code is working for registration token,actually i want the device token for particular android device.how can we get particular device token for android device
  • Nikunj
    Nikunj over 8 years
    to integrate GCM in Android you only need "device token" which you get from above code and you have to send it to server.
  • Louis
    Louis over 6 years
    InstanceID should be retrieved using InstanceID.getInstance(context).getId();