GoogleCloudMessaging - InstanceID.getInstance (), registration from client

I am new to Java programming and programming in general. Now I decided to create my own application that should use Google Cloud Messaging. And for some reason I succeeded, but then I realized that I was using the method

String regid = gcm.register(PROJECT_NUMBER);

      

which is deprecated and now I have to use tokens and instance ids. So I tried rewriting my registration to use it, but I got a problem with InstanceID.getInstancer (). I followed all the steps carefully from the documentation https://developers.google.com/instance-id/guides/android-implementation and for some reason still doesn't work.

String authorizedEntity = "79424738XXXX";
String scope = "GCM";
String token = InstanceID.getInstance().getToken(authorizedEntity,scope);

      

Android Studio tells me that the error is:

getInstance () - "getInstance (Context) on InstanceID cannot be applied until 0"

Thanks in advance!

+3


source to share


1 answer


Here is a working example at https://developers.google.com/cloud-messaging/android/start

git clone https://github.com/googlesamples/google-services.git

      

There they do in RegistrationIntentService.java



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

      

gcm_defaultSenderId

- your project id ( 79424738XXXX

)

this

- this is RegistrationIntentService.this

+4


source







All Articles