我对 Java 编程和一般编程还很陌生。现在我决定制作我自己的应用程序,该应用程序应该使用谷歌云消息传递。不知何故我成功了,但后来我意识到我使用了方法
String regid = gcm.register(PROJECT_NUMBER);
已弃用,我现在应该使用 token 和 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
)
this
是 RegistrationIntentService.this
关于java - GoogleCloudMessaging - InstanceID.getInstance(),从客户端注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30687052/