GoogleCloudMessaging - InstanceID.getInstance(), registering from client

僤鯓⒐⒋嵵緔 提交于 2019-12-07 18:04:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!