Google Cloud Messaging registration id expiration

后端 未结 4 719
野性不改
野性不改 2020-12-30 10:53

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 Goo

相关标签:
4条回答
  • 2020-12-30 11:18

    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.

    0 讨论(0)
  • 2020-12-30 11:22

    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'];
    }
    
    0 讨论(0)
  • 2020-12-30 11:23

    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)
    }
    
    0 讨论(0)
  • 2020-12-30 11:30

    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.

    0 讨论(0)
提交回复
热议问题