9

I am confused on relationship between registration id and tokens. In the tutorial for GCM from Google, we register for a registration id in the beginning. However, we also get a token. Now, in the diagrams, we send the registration id to the targeted server. However, do we also send the token? I know that the token is derived from the registration id. Is the token used as an authentication mechanism between GCM and the app and the server never knows about the token?

mrQWERTY
  • 4,039
  • 13
  • 43
  • 91

3 Answers3

19

If you are looking for a basic knowledge about Google Cloud Messaging, IMO, you can refer to the following:

Basically, you need to do the steps:

  1. Create a new project at Google Developers Console . At this step, for simplicity, you just need to take note of 2 values: Project Number, which will be used as SENDER_ID in the client project; and API server key (created at Credentials), which will be used as API_KEY in the server project.
  2. Create a new simple Android project for server side (with basic source code as my answer in the following links).
  3. Create a new simple Android project for client side (with basic source code as my answer in the following links, I customized from the original source at Google Cloud Messaging - GitHub).
  4. Run the client app, you will get the registration token (means that your device has successfully registered). Then, paste (hard-code) this token at CLIENT_REGISTRATION_TOKEN variable in server app (or write code to send this token to server app).

You can read more at the following questions, one of them you have read before with one of your previous questions:

  1. How to implement a GCM Hello World for Android using Android Studio
  2. Adding Google Cloud Messagin (GCM) for Android - Registration process

For more information:

Key Concepts from Google Cloud Messaging: Overview

Credentials

  • Sender ID A unique numerical value created when you configure your API project (given as "Project Number" in the Google Developers Console). The sender ID is used in the registration process to identify an app server that is permitted to send messages to the client app.
  • API Key An API key saved on the app server that gives the app server authorized access to Google services. In HTTP, the API key is included in the header of POST requests that send messages. In XMPP, the API key is used in the SASL PLAIN authentication request as a password to authenticate the connection. You obtain the API key when you configure your API project.
  • Registration Token An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.

Hope this helps!

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
  • Ok, so the server only cares about the registration id. It doesn't care about the token at all. – mrQWERTY Nov 03 '15 at 03:04
  • 3
    The registration token = the registration Id, which is got at client app, then you must provide it to server app – BNK Nov 03 '15 at 03:06
  • Perhaps you mean SENDER_ID, which as I said in the 1st step – BNK Nov 03 '15 at 03:07
  • 1 Question, Do i need to put `Configuration File` (that is downloaded from `GCM`) in my android project or is it for Server Application? – Taha Kirmani Jun 22 '16 at 07:46
  • 1
    @TahaKirmani do you mean `google-services.json` file? Save it at `YourProject\app` path – BNK Jun 22 '16 at 07:53
  • Thanks, I have added that file there, do i need to perform any further steps in order to connect it with the App? – Taha Kirmani Jun 22 '16 at 07:57
  • 1
    @TahaKirmani `with the app`, can you clarify that? I think you can refer 2 links in my answer – BNK Jun 22 '16 at 08:01
  • Thanks, i got that :) – Taha Kirmani Jun 22 '16 at 08:05
  • @TahaKirmani if you need sample projects, please go to https://github.com/ngocchung/GCMAndroid (client app) and https://github.com/ngocchung/gcmserver (server app) – BNK Jun 22 '16 at 08:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115277/discussion-between-taha-kirmani-and-bnk). – Taha Kirmani Jun 22 '16 at 08:10
  • Thanks for the links, sorry for making it long, 1 last Question, Let say i don't add conditions and each time user loads activity `instanceID.getToken()` method runs, Will it add new token Id each time, or it will identify device and won't generate a new token ID each time from same device ? – Taha Kirmani Jun 22 '16 at 08:20
  • @TahaKirmani please read https://developers.google.com/android/reference/com/google/android/gms/iid/InstanceID.html#public-methods, `public String getToken (String authorizedEntity, String scope) 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.` I have not checked for a long time, however, I think a new token will be generated, you can try :) – BNK Jun 22 '16 at 08:28
  • To someone who has just downvoted, can you give me your reason please? – BNK Aug 23 '16 at 02:09
14

GCM now uses the concept of an InstanceID which represents a single install of an app on a device (Android or iOS). Each InstanceID can issue several tokens. These tokens are used to identify the InstanceID and can expire and be refreshed.

On the client device, you initialize an InstanceID, then with that InstanceID you generate a token (registration token). You send that token to your server, which uses the token to send messages to the InstanceID (installed application). If that token is invalidated for any reason like the application is uninstalled or the token is compromised, a new token should be generated and sent to your server.

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
-1

I am still successfully registering with registration Id method until 11 May 2016

if (checkPlayServices()) {
            gcm = GoogleCloudMessaging.getInstance(this);
            regid = getRegistrationId(context);

            if (regid.isEmpty()) {
                registerInBackground();
            }
        } else {
            Log.i(TAG, "No valid Google Play Services APK found.");
        }

APA91bHLUfr71D6K7VTrRH3LGiLFxGNr3qRi3xOB_yNl0fLYsqhlgYXxHzOhQx2WKgqZI3sqxa1ZPORa0-5YBZ1_OFLm9cEg1bTh7wtrpCsHW91MSs2BMIXrHEqyjj2TeoVxnAzA5U8s

Son Nguyen
  • 11
  • 1