RagHaven
RagHaven

Reputation: 4327

Google Cloud Messaging registration id expiration

I am using Google Cloud Messaging for my Android application and I am trying to understand when the registration id expires. From this post I was able to understand that Google tends to refresh the ID at some time. I am curious how my application will know when the id gets refreshed? If Google decides to refresh the ID and my server is till sending the message to the old ID I dont think the message will get sent. So would I have to try and register every time and see if the ids are same?

Also the same post says that the id would get refreshed when the app version changes, but on changing the version through the manifest the registration id did not change. So what is the point on trying to register again of the version changes?

EDIT Here is the server side. Where exactly would the canonical id be stored?

Server side code:

<?php
// Message to be sent
$message = $_POST['message'];
 
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
 
$fields = array(
                'registration_ids'  => array($_POST['registrationIDs']),
                'data'              => array( "message" => $message ),
                );
 
$headers = array( 
                    'Authorization: key=' . $_POST['apiKey'],
                    'Content-Type: application/json'
                );
 
// Open connection
$ch = curl_init();
 
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
 
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
 
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
 
// Execute post
$result = curl_exec($ch);
 
// Close connection
curl_close($ch);
 
echo $result;
 
?>

Upvotes: 10

Views: 12434

Answers (4)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

To obtain the latest GCM ID Here is my work around.

$result = curl_exec($ch);
$result = json_decode($result, true);
$GCMIDChanged = $result['canonical_ids'];
if($GCMIDChanged)
{
    $NewGCMId = $result['results'][0]['registration_id'];
    //Update your DB with $NewGCMId
}

In the above code, I am checking whether the canonical_ids exists in the result.

If the canonical_ids exists then i am getting the new registration_id

So, Now the system will update automatically the New Gcm Id whenever it is changed.

Update :

The below code can be used if the GCM Id is changed multiple times

$GCMIDChanged = $result['canonical_ids'];
if($GCMIDChanged)
{
    $NewGCMIdList = end($result['results']);
    $NewGCMId =  $NewGCMIdList['registration_id'];
}

Upvotes: 0

Shivanand Darur
Shivanand Darur

Reputation: 3224

As per http://developer.android.com/google/gcm/client.html#sample-register, unlike previous versions of GCM and C2DM, Google itself does not refresh the registration. Once you have the registration id from the initial registration you are good to go, except for one case: you do still need to re-register when the user upgrades to a new version (this case is also handled in the example in the link above).

When an application is updated, it should invalidate its existing registration ID, as it is not guaranteed to work with the new version. Because there is no lifecycle method called when the application is updated, the best way to achieve this validation is by storing the current application version when a registration ID is stored.

Upvotes: 0

Archie.bpgc
Archie.bpgc

Reputation: 24012

I am trying to understand when the registration id expires

GCM server usually refreshes(So your old registration id is expired) the registration ids. When exactly this happens is not documented anywhere. But you will be notified when this happens.

How are we notified?

When you send a notification to a registration id which is expired, for the 1st time the message(notification) will be delivered but you will get a new registration id with the name canonical id. Which mean, the registration id you sent the message to has been changed to this canonical id, so change it on your server side as well.


If Google decides to refresh the ID and my server is till sending the message to the old ID I dont think the message will get sent

Like I said, it will be sent for the 1st time, even after expiry.


Also the same post says that the id would get refreshed when the app version changes

I don't think this happens for sure. But even if it happens(registration id changes) its the same scenario as explained above, so all you need is to take care of the canonical id.

EDIT

$result = curl_exec($ch);
$result = json_decode($result);
$canonical_ids_count = $result->canonical_ids;
if($canonical_ids_count){
    //update your DB by replacing the registration id with 
    //the canonical id(new registration id)
}

Upvotes: 8

Milan
Milan

Reputation: 1875

I am not exactly sure when GCM regId gets refreshed but, GCM Client documentation states that "Check if app was updated; if so, it must clear the registration ID, since the existing regID is not guaranteed to work with the new app version." So yes it might still be using old regId but, it's not guranteed that it will not have a new regId.

Upvotes: 0

Related Questions