什么是授权实体?在自己的应用程序中找不到 gcm_defaultSenderId [英] What is authorizedEntity? Can't find gcm_defaultSenderId in own app

查看:31
本文介绍了什么是授权实体?在自己的应用程序中找不到 gcm_defaultSenderId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Google Cloud Messaging 运行我的应用.我正在关注可在 github 上找到的 Google Cloud Messaging Quickstart App.

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 服务提供注册令牌,以便我们的应用程序实例可以与云通信.

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.

我发现这行代码:

RegistrationIntentService.java::onHandleIntent(Intent intent): 

InstanceID instanceID = InstanceID.getInstance(this);
String gcmRegistrationToken = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

让我困惑的部分是这个值:R.string.gcm_defaultSenderId它在他们的快速入门应用程序中定义,但它是自动生成的.

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.

我应该如何让我的应用产生该价值?

How am I supposed to get my app to generate that value?

我在此处查找 InstanceID.getToken 的文档a> getToken(java.lang.String, java.lang.String)

InstanceID.gettoken 返回一个令牌,该令牌授权实体(例如:云服务)代表由实例 ID 标识的应用程序执行操作.这类似于 OAuth2 令牌,不同之处在于它适用于应用程序实例而不是用户.

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.

函数头看起来像:

public String getToken (String authorizedEntity, String scope)

我看到 getToken 想要的第一个参数是 String authorizedEntity.那么,这个authorizedEntity String 应该是什么?
它清楚地标识了发出请求的应用程序的实例,但我应该如何生成它?在快速入门应用程序中,我在 res/value/strings.xml 中找不到它的定义,我只能在 R.java 和 app/build/generated/res/中找到它定义google-services/debug/values/values.xml

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

看起来像:

<resources>
<string name="gcm_defaultSenderId">175643285</string>
</resources>

该文件中只有一个字符串,并且该文件深埋在项目结构中.我在代码中找不到以编程方式生成此 gcm_defaultSenderId 的任何地方.

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"并没有给出任何结果.我正在尝试在我自己的应用程序中实现 Google Cloud Messaging,所以当然我自己的应用程序不会自动知道生成该字符串.我应该如何制作那个身份证号码?

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?

这就是为什么我认为了解 InstanceID.getToken 想要的这个授权实体字符串很重要的原因,以便我可以正确地生成一个给 getToken.也许我的想法完全错误,也许我不应该生成gcm_defaultSenderId,但我知道我不应该更改R.java,并且values.xml文件也在生成"下文件夹.

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.

请帮忙?如果我在搜索中找到答案,我会很乐意发布答案.非常感谢任何帮助,请注意:我的项目已从 Eclipse 导出到 Gradle,因此它仍将具有 Eclipse 项目/文件夹结构,不会造成任何问题,但 values.xml 文件位于不同的位置.

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.

推荐答案

R.string.gcm_defaultSenderId 值由 Gradle google-services 插件生成,该插件使用 google-services.json 文件,定义了常量.

The R.string.gcm_defaultSenderId value is generated by the Gradle google-services plugin which uses a google-services.json file with defined constants.

该插件在 Gradle 中应用:

The plugin is applied in Gradle:

apply plugin: 'com.google.gms.google-services'

欲了解更多信息,请查看在 Android 上实现 GCM 客户端并查看如何获取 google-services.json 文件和设置 Gradle &开发者控制台中的应用.

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.

这篇关于什么是授权实体?在自己的应用程序中找不到 gcm_defaultSenderId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆