I am trying to get my app running with Google Cloud Messaging. I am following the Google Cloud Messaging Quickstart App which can be found here on github.
我正在尝试使用Google Cloud Messaging运行我的应用。我正在关注可在github上找到的Google Cloud Messaging Quickstart App。
In their quickstart app at some point we ask the Google Cloud Messaging service for a registration token so that this instance of our app can talk to the cloud.
在他们的快速入门应用程序中,我们会向Google Cloud Messaging服务部门申请注册令牌,以便我们的应用程序实例可以与云对话。
I find this line of code:
我发现这行代码:
RegistrationIntentService.java::onHandleIntent(Intent intent):
InstanceID instanceID = InstanceID.getInstance(this);
String gcmRegistrationToken = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
The part that is confusing me is this value: R.string.gcm_defaultSenderId
It is defined in their quickstart app, but it is automatically generated.
令我困惑的部分是这个值:R.string.gcm_defaultSenderId它在他们的快速入门应用程序中定义,但它是自动生成的。
How am I supposed to get my app to generate that value?
我怎么能让我的应用程序生成该值?
I look up the docs for InstanceID.getToken which is here getToken(java.lang.String, java.lang.String)
我查找InstanceID.getToken的文档,这里是getToken(java.lang.String,java.lang.String)
InstanceID.getoken
returns a token that authorizes an Entity (example: cloud service) to perform an action on behalf of the application identified by Instance ID. This is similar to an OAuth2
token except, it applies to the application instance instead of a user.
InstanceID.getoken返回一个令牌,该令牌授权实体(例如:云服务)代表由实例ID标识的应用程序执行操作。这类似于OAuth2令牌,但它适用于应用程序实例而不是用户。
The function header looks like:
函数标题如下所示:
public String getToken (String authorizedEntity, String scope)
I see that the first arg that getToken
wants is String authorizedEntity
. so, what is this authorizedEntity String supposed to be?
It clearly identifies the instance of the app making the request, but how am I supposed to generate it? In the quickstart app, I can't find it defined in res/value/strings.xml
, I can only find it defined in R.java and app/build/generated/res/google-services/debug/values/values.xml
我看到getToken想要的第一个arg是String authorizedEntity。那么,这个authorizedEntity字符串应该是什么?它清楚地标识了发出请求的应用程序的实例,但我应该如何生成它?在快速入门应用程序中,我找不到它在res / value / strings.xml中定义,我只能在R.java和app / build / generated / res / google-services / debug / values / values中找到它。 XML
It looks like:
看起来像:
<resources>
<string name="gcm_defaultSenderId">175643285</string>
</resources>
There is just that one string in that file, and that file is buried way deep in the project structure. I can't find anywhere in the code where this gcm_defaultSenderId
is being generated programmatically.
该文件中只有一个字符串,该文件深埋在项目结构中。我无法在代码中找到以编程方式生成此gcm_defaultSenderId的任何位置。
I'm confused because, how was I supposed to know that string was there? I never defined that string, and googling for "cannot resolve gcm_DefaultSenderId
" gives no results. I'm trying to implement Google Cloud Messaging in my own app, so of course my own app is not going to automatically know to generate that string. How am I supposed to make that id number?
我很困惑,因为我怎么知道字符串在那里?我从来没有定义过这个字符串,并且谷歌搜索“无法解析gcm_DefaultSenderId”没有给出任何结果。我正在尝试在自己的应用中实施Google Cloud Messaging,因此我自己的应用程序当然不会自动知道生成该字符串。我该怎么做那个身份证号码?
This is why I think it's important that I understand what this authorizedEntity string that InstanceID.getToken wants, so that I can properly generate one to give to getToken. Perhaps my idea is completely wrong, perhaps I am not supposed to generate gcm_defaultSenderId, but I know that I am not supposed to alter R.java, and the values.xml
file is also under a "generated" folder.
这就是为什么我认为我理解InstanceID.getToken想要的authorizedEntity字符串是多么重要,以便我可以正确生成一个给getToken。也许我的想法是完全错误的,也许我不应该生成gcm_defaultSenderId,但我知道我不应该改变R.java,而且values.xml文件也在“generated”文件夹下。
Help please? If I find the answer in my searches, I will happily post the answer. Any help much appreciated, note: my project was exported to Gradle from Eclipse, so it will still have the Eclipse project/folder structure, that shouldn't cause any problems, but the values.xml
file is in a different place.
请帮助?如果我在搜索中找到答案,我会很乐意发布答案。非常感谢任何帮助,请注意:我的项目从Eclipse导出到Gradle,因此它仍然具有Eclipse项目/文件夹结构,不应该导致任何问题,但values.xml文件位于不同的位置。
2 个解决方案
#1
37
The R.string.gcm_defaultSenderId
value is generated by the Gradle google-services plugin which uses a google-services.json
file with defined constants.
R.string.gcm_defaultSenderId值由Gradle google-services插件生成,该插件使用带有已定义常量的google-services.json文件。
The plugin is applied in Gradle:
该插件在Gradle中应用:
apply plugin: 'com.google.gms.google-services'
For more info check Implementing GCM Client on Android and see how to get the google-services.json
file and set-up Gradle & app in developer console.
有关详细信息,请查看在Android上实施GCM客户端,了解如何获取google-services.json文件并在开发人员控制台中设置Gradle&app。
#2
12
It is the project id that we need to fill in place of that string. Please refer to the following link.
我们需要填写该字符串的项目ID。请参考以下链接。
https://developers.google.com/instance-id/guides/android-implementation
Generating tokens requires a Project ID generated by the Google Developers Console.
生成令牌需要Google Developers Console生成的项目ID。
String authorizedEntity = PROJECT_ID; // Project id from Google Developers Console
String scope = “GCM”; // e.g. communicating using GCM, but you can use any
// URL-safe characters up to a maximum of 1000, or
// you can also leave it blank.
String token = InstanceID.getInstance().getToken(authorizedEntity,scope);
#1
37
The R.string.gcm_defaultSenderId
value is generated by the Gradle google-services plugin which uses a google-services.json
file with defined constants.
R.string.gcm_defaultSenderId值由Gradle google-services插件生成,该插件使用带有已定义常量的google-services.json文件。
The plugin is applied in Gradle:
该插件在Gradle中应用:
apply plugin: 'com.google.gms.google-services'
For more info check Implementing GCM Client on Android and see how to get the google-services.json
file and set-up Gradle & app in developer console.
有关详细信息,请查看在Android上实施GCM客户端,了解如何获取google-services.json文件并在开发人员控制台中设置Gradle&app。
#2
12
It is the project id that we need to fill in place of that string. Please refer to the following link.
我们需要填写该字符串的项目ID。请参考以下链接。
https://developers.google.com/instance-id/guides/android-implementation
Generating tokens requires a Project ID generated by the Google Developers Console.
生成令牌需要Google Developers Console生成的项目ID。
String authorizedEntity = PROJECT_ID; // Project id from Google Developers Console
String scope = “GCM”; // e.g. communicating using GCM, but you can use any
// URL-safe characters up to a maximum of 1000, or
// you can also leave it blank.
String token = InstanceID.getInstance().getToken(authorizedEntity,scope);