Java – GoogleCloudMessaging – InstanceID.getInstance(), registered from the client

GoogleCloudMessaging – InstanceID.getInstance(), registered from the client… here is a solution to the problem.

GoogleCloudMessaging – InstanceID.getInstance(), registered from the client

I’m still new to Java programming and programming in general. Now I decided to make my own app, which should use Google Cloud Messaging. Somehow I succeeded, but then I realized I was using methods

String regid = gcm.register(PROJECT_NUMBER);

Deprecated, I should now use token and instanceID. So I tried rewriting my registration to use it, but I ran into issues with InstanceID.getInstancer(). I carefully followed the documentation All the steps in https://developers.google.com/instance-id/guides/android-implementation somehow it 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) in InstanceID cannot be applied
to 0”

Thanks in advance!

Solution

In https://developers.google.com/cloud-messaging/android/start has a working example

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

There they .java the RegistrationIntentService

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

gcm_defaultSenderId is your project ID (79424738XXXX).

this

is RegistrationIntentService.this

Related Problems and Solutions