GoogleCloudMessaging - InstanceID.getInstance(),从客户端注册 [英] GoogleCloudMessaging - InstanceID.getInstance(), registering from client
问题描述
我对 Java 编程和一般编程非常陌生.现在我决定制作我自己的应用程序,该应用程序应该使用 Google Cloud Messaging.不知何故我成功了,但后来我意识到我使用了方法
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);
这已被弃用,我现在应该使用令牌和 InstanceID.所以我尝试重写我的注册来使用它,但我遇到了 InstanceID.getInstancer() 的问题.我仔细地遵循了文档 https://developers.google.com/instance- 中的所有步骤id/guides/android-implementation 不知何故它仍然不起作用.
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 告诉我错误是:
Android Studio tells me that error is:
getInstance() - "InstanceID 中的getInstance (Context) 不能应用到 0"
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
)
gcm_defaultSenderId
is your project id (79424738XXXX
)
this
是 RegistrationIntentService.this
这篇关于GoogleCloudMessaging - InstanceID.getInstance(),从客户端注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!