Java-GoogleCloudMessaging-InstanceID.getInstance(),从客户端注册
作者:互联网
我对Java编程和一般编程非常陌生.现在,我决定制作自己的应用程序,该应用程序应该利用Google Cloud Messaging.我以某种方式成功了,但是后来我意识到我使用了方法
String regid = gcm.register(PROJECT_NUMBER);
已弃用,现在我应该使用令牌和InstanceID.因此,我尝试重写注册以使用它,但我遇到InstanceID.getInstancer()的问题.我仔细地遵循了文档https://developers.google.com/instance-id/guides/android-implementation的所有步骤,以某种方式它仍然无法正常工作.
String authorizedEntity = "79424738XXXX";
String scope = "GCM";
String token = InstanceID.getInstance().getToken(authorizedEntity,scope);
Android Studio告诉我错误是:
getInstance() – “getInstance (Context) in InstanceID cannot be applied
to 0”
提前致谢!
解决方法:
有一个工作示例在https://developers.google.com/cloud-messaging/android/start
git clone https://github.com/googlesamples/google-services.git
在这里,它们在RegistrationIntentService.java中进行
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
gcm_defaultSenderId是您的项目ID(79424738XXXX)
这是RegistrationIntentService.this
标签:api,cloud,messaging,java,android 来源: https://codeday.me/bug/20191028/1950905.html