How to parse GCM respond to remove invalid registration id from server with php

后端 未结 3 1002
暗喜
暗喜 2020-12-29 10:20

I got a question about Google Cloud Messaging...

I send GCM to Google for 3 Registration IDs, then Google replies that 2 of the Registration IDs has been sent succes

3条回答
  •  一个人的身影
    2020-12-29 10:52

    Here's how I did it:

    $gcm_result = $gcm->send_notification($registration_ids, $message);
    $jsonArray = json_decode($gcm_result);
    
    if(!empty($jsonArray->results)){
    
        $remove_ids = array();
    
        for($i=0; $iresults);$i++){
            if(isset($jsonArray->results[$i]->error)){
                if($jsonArray->results[$i]->error == "NotRegistered"){
                    $remove_ids[$i] = "'".$registration_ids[$i]."'";
                }
            }
        }
    
        if(!empty($remove_ids)){
    
            $remove_ids_string = implode(', ',$remove_ids);
            include_once SCRIPTS.'gcm_server_php/db_functions.php';
            $dbf = new DB_Functions();
            $res = $dbf->deleteUsers($remove_ids_string);
            echo count($remove_ids)." users have unregistered from notifications since the last one was sent out.";
    
        }
    }
    

提交回复
热议问题