David Addoteye
David Addoteye

Reputation: 1641

Google Cloud Messaging for Cordova Clarification

I am implementing Google Cloud Messaging service in my cordova app. so far everything is working well. I however have some few issues bothering I wish someone can clarify them for me. 1) At this section of the code where I get the device GCM regID and further save it to on my server. I will like to know if I should call this script and thus save GCM regID to my server anytime the user opens the App or it should be called and saved once..

function onNotification(e) {
switch( e.event )
{
case 'registered':
    if ( e.regid.length > 0 )
    {                    
        console.log("regID = " + e.regid);
    }
   break;
   }
 }

2) I have noticed on the console that my registered ID sometime changes. I will like to know why that happens, whether it is normal and also if I should be updating the users GCM regID on my server.

I wil be glad if anyone can clarify these for me. Thank you

Upvotes: 2

Views: 1720

Answers (3)

Ajoy
Ajoy

Reputation: 1848

  1. You should call the script every time. Only update the registration ID if it has changed.

  2. Read my answer. Also, this answer for more details

Upvotes: 2

Abdul Hameed
Abdul Hameed

Reputation: 938

for (1) it should be called and saved on server once. as am working in my applications and save this id to server once, and all things working fine. for (2) however in development the registrations id's sometimes changes. The reason is that in development we uninstall or reinstall the application completely. thus makes the registration id's to change.because registration id assigned on app installation. but for exceptional case also see this.

Upvotes: 5

Arthur Thompson
Arthur Thompson

Reputation: 9225

You only need to send your registration token to your server once. If your registration token changes you should send the changed registration token to your server.

The registration token may change if the application is uninstalled and reinstalled or if GCM determines that the token has been compromised in some way.

You should register a service to listen for registration token changes it should extend InstanceIDListenerService and you override its onTokenRefresh method like this:

@Override
public void onTokenRefresh() {
    // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
    InstanceID instanceID = InstanceID.getInstance(this);
    String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
            GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
    Log.i(TAG, "GCM Registration Token: " + token);

    // Send token to server.
}

Consider GCM getting started documentation for more details.

Upvotes: 1

Related Questions