问题
I'm pretty new to programming in Java and generally programming. Right now I decided to make my own app that is supposed to utilize Google Cloud Messaging. And somehow I succeeded but then I realized I used method
String regid = gcm.register(PROJECT_NUMBER);
that is deprecated and I should now use tokens and InstanceIDs. So I tried rewriting my registration to use it but I encountered a problem with InstanceID.getInstancer(). I carefully followed all steps from documentation https://developers.google.com/instance-id/guides/android-implementation and somehow it still doesn't work.
String authorizedEntity = "79424738XXXX";
String scope = "GCM";
String token = InstanceID.getInstance().getToken(authorizedEntity,scope);
Android Studio tells me that error is:
getInstance() - "getInstance (Context) in InstanceID cannot be applied to 0"
Thanks in advance!
回答1:
There's a working example at https://developers.google.com/cloud-messaging/android/start
git clone https://github.com/googlesamples/google-services.git
In 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 is your project id (79424738XXXX)
this is the RegistrationIntentService.this
来源:https://stackoverflow.com/questions/30687052/googlecloudmessaging-instanceid-getinstance-registering-from-client