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;
?>
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With