27

Could somebody please help me understand what the gcm_defaultSenderId is in the following code (found in onHandleIntent in RegistrationIntentService.java):

InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            // [END get_token]
            Log.i(TAG, "GCM Registration Token: " + token);

This is from the Google sample app for implementing GCM into your app, and it doesn't even compile in their app! I'm sure it's something specific to each app. I've already added the GCM API to my application, just don't know what this string is supposed to be! Thanks!

Brandon
  • 1,886
  • 2
  • 17
  • 28
  • 1
    After gaining further understanding of this problem, I realized that you must create the configuration file (google-services.json) and import that. Then the project resolves the senderID based on the plugin mentioned by @Kroikie Please let me know if you need any more clarification – Brandon Jun 12 '15 at 17:42

6 Answers6

28

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.

Like Vesko said this is your Sender ID which in this case is the Project Number in your dev console project. The google-services plugin extracts this from your downloaded project configuration file.

Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124
Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
8

Quoting THIS document, where you can find details about that implementation:

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);

So as you can see, the first param you should pass to getToken() is the authorizedEntity, which should be your project id from Google Developers :)

Even if the project in GitHub had that string, it wouldn't server you any good, as this authorizedEntity is something unique for each app.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Vesko
  • 3,750
  • 2
  • 23
  • 29
  • Yeah, I knew that it was unique to each app (see my question above, tehe), but thanks! This was the answer I ended up finding [here](http://stackoverflow.com/questions/11294602/android-gcm-sender-id). I'll accept your answer, thanks for posting relevant code and a link! – Brandon Jun 07 '15 at 17:23
7

I hate those buggy Gradle plugins, and trying to get the google-services plugin to operate in a project with multiple flavours is no fun either.

I ended up getting the Sender Id by name:

InstanceID instanceID = InstanceID.getInstance(this);
String gcmDefaultSenderId = getString( getResources().getIdentifier("gcm_defaultSenderId", "string", this.getPackageName()) );
String token = instanceID.getToken( gcmDefaultSenderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Vaiden
  • 15,728
  • 7
  • 61
  • 91
1

To solve this you just have to add apply plugin: 'com.google.gms.google-services' to your gradle.app module and classpath 'com.google.gms:google-services:1.3.0' to the dependencies of your gradle.app project

and of course, dont forget to add compile 'com.google.android.gms:play-services-gcm:8.1.0' to your gradle

Gabriel Ferreira
  • 435
  • 6
  • 10
1

There is one more important thing, Create the google-services.json file.

Just in case someone else stumbles upon this issue.

Lisandro
  • 10,543
  • 1
  • 24
  • 29
0

Place the file google-services.json under \app directory, not inside src or other places. Be sure the GCM plugin is applied. Build the project, and the string will now resolve.

pedronveloso
  • 915
  • 9
  • 16