Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is authorizedEntity? Can't find gcm_defaultSenderId in own 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.

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.

like image 601
sitting-duck Avatar asked Jun 04 '15 20:06

sitting-duck


People also ask

What is gcm_defaultSenderId?

The gcm_defaultSenderId is a string is included by the google-services gradle plugin. Be sure you have the: apply plugin: 'com.google.gms.google-services' in your build. gradle file. This plugin should be available in the latest version of the build tools.


2 Answers

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.

like image 167
peter.bartos Avatar answered Sep 19 '22 13:09

peter.bartos


It is the project id that we need to fill in place of that string. Please refer to the following link.

https://developers.google.com/instance-id/guides/android-implementation

Generating tokens requires a Project ID generated by the Google Developers Console.

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); 
like image 34
Ramesh Avatar answered Sep 20 '22 13:09

Ramesh