how to get a Token from InstanceID in GCM

1.2k views Asked by At

I am learning how to send and receive push notifications via GCM, I followed this tutorial

and there is a class called GCMRegistrationIntentService posted below. concerning the following line:

InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
//Getting the token from the instance id
token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

what is R.string.gcm_defaultSenderId)? I tried to find it in strings.xml, but such an entry was not available

could please clarify and explain?

2

There are 2 answers

0
Enamul Haque On

You can use FirebaseInstanceIdService to get token.. like bellow

       public class FirebaseIDService extends FirebaseInstanceIdService {


     @Override
       public void onTokenRefresh() {
         String token = FirebaseInstanceId.getInstance().getToken();


      }
   }

Don't forget to add bellow code to andoridmainfest.xml

  <service android:name=".FirebaseIDService" android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
0
CaHa On

That string contains your project_number and is auto-generated by Gradle based on your google-services.json file:

The main result of the JSON processing is to produce two XML files which you can reference as Android resources in your Java code. Below is an example of each file:

app/build/generated/res/google-services/{build_type}/values/values.xml

Source: https://developers.google.com/android/guides/google-services-plugin