29

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.

sitting-duck
  • 961
  • 1
  • 10
  • 22
  • 1
    The sender ID is from the Developers console, but if you followed the quickstart, it was generated for you in the google-services.json file that you copied into your project directory. – Koh Jun 04 '15 at 23:22
  • You can drop the R.string id and get the value by name: http://stackoverflow.com/a/38995785/606351 – Vaiden Aug 18 '16 at 10:43

2 Answers2

36

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.

peter.bartos
  • 11,855
  • 3
  • 51
  • 62
  • 2
    This is the right answer. The google-services.json file must be on the app root folder in order the Gradle plugin can auto generate the R.string.gcm_defaultSenderId. In order to get the google-services.json file follow this official guide: https://developers.google.com/cloud-messaging/android/client – Sotti Jul 06 '15 at 14:45
  • I have that plugin installed but that string value does not resolve – Captain Prinny Mar 06 '16 at 19:17
14

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);
Ramesh
  • 1,287
  • 1
  • 9
  • 14
  • you saved me a ton of searching sir :) – Kushan Feb 10 '17 at 13:38
  • Glad to know Kushan :) But If you are on latest google services use @peter's answer ! – Ramesh Feb 10 '17 at 16:26
  • I was actually looking to use the InstanceId api and use it to delete tokens.. that's why i needed the authEntity. Peter's answer was what i was using. It threw a shit ton problems at my face due to default use of app analytics that the service has and by default firebase api turns it on. Each service of mine had an over head of 60-70 mb due to that nonsense – Kushan Feb 10 '17 at 16:44