What is authorizedEntity? Can't find gcm_defaultSenderId in own app

sitting-duck picture sitting-duck · Jun 4, 2015 · Viewed 12.8k times · Source

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.

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.

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.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.

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

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.

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?

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.

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.

Answer

peter.bartos picture peter.bartos · Jul 1, 2015

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

The plugin is applied in 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.