什么是authorizedEntity?找不到gcm_defaultSenderId在自己的应用程序自己的、找不到、应用程序、authorizedEntity

2023-09-06 01:41:48 作者:再成熟的表演不如不见

我试图让我的应用程序与谷歌云消息传递运行。我下面的谷歌云消息传递快速启动应用程序,它可以在这里找到在 github上。

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.

在在某些时候他们的快速启动应用程序,我们要求注册标记的谷歌云消息传递服务,使我们的应用程序的这个实例可以谈云计算。

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.

我觉得这行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);

这是混淆了我的部分是这个值: 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这是here 为gettoken(java.lang.String中,java.lang.String中)

I look up the docs for InstanceID.getToken which is here getToken(java.lang.String, java.lang.String)

InstanceID.getoken 返回一个令牌授权的实体(例如:云服务),以代表确定实例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 要的是字符串authorizedEntity 。 所以,这是什么authorizedEntity字符串应该是什么? 它清楚地标识发出请求的应用程序的实例,但是我怎么产生的呢?在快速启动应用程序,我无法找到它在 RES /价值/ strings.xml中,我只能找到它在R.java定义和应用程序/编译/生成/ RES /谷歌服务/调试/价值/ 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>

现在只是一个字符串的文件,而该文件被埋没的方式深深的项目结构。 我找不到在code任何地方本 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.

我很困惑,因为,怎么是我应该知道,串在那里?我从来没有定义字符串,并使用Google的无法解析 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?

这就是为什么我认为,重要的是我明白这是什么authorizedEntity字符串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.

帮助吗?如果我发现我的搜索答案,我会很高兴发布了答案。任何帮助非常AP preciated,注:我的项目是出口到摇篮从的Eclipse ,所以它仍然有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 值由摇篮谷歌服务插件,它使用生成的谷歌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.

该插件在摇篮应用:

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

有关详细信息检查 在Android 实施GCM客户端,看看如何获​​得谷歌services.json 文件和设置摇篮和放大器;应用程序的开发者控制台。

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.