2013-06-04 5 views
12

Ho una domanda su Google Cloud Messaging ...come analizzare GCM rispondere per rimuovere valida registrazione ID dal server con PHP

mando GCM a Google per 3 ID di registrazione, allora Google risponde che 2 della registrazione Gli ID sono stati inviati con successo e uno no, perché l'ID di registrazione era sbagliato!

Ma non mi dice che ID di registrazione non è stato inviato ...

Ora ecco la mia domanda: come posso analizzare la risposta di Google GCM per ottenere ciò che ID La registrazione non è stato inviato? Google ha un'API o qualcosa che possa dargli "multicat_id" e mi dice quale ID di registrazione ha avuto un problema?

Qualsiasi aiuto sarebbe molto apprezzato, sto solo così confusa :)

risposta

15

Si basa sull'ordine:

Supponiamo di inviare questo:

{ "collapse_key": "score_update", 
    "time_to_live": 108, 
    "delay_while_idle": true, 
    "data": { 
    "score": "4x8", 
    "time": "15:16.2342" 
    }, 
    "registration_ids":["4", "8", "15", "16", "23", "42"] 
} 

E ottenere questa risposta da Google:

{ "multicast_id": 216, 
    "success": 3, 
    "failure": 3, 
    "canonical_ids": 1, 
    "results": [ 
    { "message_id": "1:0408" }, 
    { "error": "Unavailable" }, 
    { "error": "InvalidRegistration" }, 
    { "message_id": "1:1516" }, 
    { "message_id": "1:2342", "registration_id": "32" }, 
    { "error": "NotRegistered"} 
    ] 
} 

Ciò significa che il terzo ID di registrazione inviato (15) è in valido e il sesto (42) non è registrato. Entrambi dovrebbero essere rimossi dal tuo DB.

+0

Risposta molto utile. Basta dividere la risposta con "risultato" e dividere il secondo argomento con "," e ripetere su tutto ciò che è necessario. –

9

Ecco come ho fatto:

$gcm_result = $gcm->send_notification($registration_ids, $message); 
$jsonArray = json_decode($gcm_result); 

if(!empty($jsonArray->results)){ 

    $remove_ids = array(); 

    for($i=0; $i<count($jsonArray->results);$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."; 

    } 
} 
+0

E in caso qualcuno stia cercando la funzione Elimina utenti qui. public function deleteUsers ($ remove_ids_string) { \t $ registration_ids = explode (",", $ remove_ids_string); \t foreach ($ registration_ids as $ word) \t \t { \t \t \t \t \t \t $ risultato = mysql_query ("eliminare dalla notification_tbl DOVE gcm_id = '$ parola'"); \t \t \t \t \t} \t \t } –

2

In base a quello che ho letto qui:

http://developer.android.com/google/gcm/http.html#error_codes http://developer.android.com/google/gcm/server-ref.html#error-codes

ho sviluppato come segue (non ancora testato):

$result = $gcm->send_notification($registration_ids, $message); 
$jsonArray = json_decode($result); 

if($jsonArray->canonical_ids != 0 || $jsonArray->failure != 0){ 
    if(!empty($jsonArray->results)) 
    { 
     for($i = 0 ; $i<count($jsonArray->results) ; $i++){ 
      $result = $jsonArray->results[$i]; 
      if(isset($result->message_id) && isset($result->registration_id)) 
      { 
       // You should replace the original ID with the new value (canonical ID) in your server database 
      } 
      else 
      { 
       if(isset($result->error)) 
       { 
        switch ($result->error) 
        { 
         case "NotRegistered": 
         case "InvalidRegistration": 
          // You should remove the registration ID from your server database 
          break; 
         case "Unavailable": 
         case "InternalServerError": 
          // You could retry to send it late in another request. 
          break; 
         case "MissingRegistration": 
          // Check that the request contains a registration ID 
          break; 
         case "InvalidPackageName": 
          // Make sure the message was addressed to a registration ID whose package name matches the value passed in the request. 
          break; 
         case "MismatchSenderId": 
          // Invalid SENDER_ID 
          break; 
         case "MessageTooBig": 
          // Check that the total size of the payload data included in a message does not exceed 4096 bytes 
          break; 
         case "InvalidDataKey": 
          // Check that the payload data does not contain a key that is used internally by GCM. 
          break; 
         case "InvalidTtl": 
          // Check that the value used in time_to_live is an integer representing a duration in seconds between 0 and 2,419,200. 
          break; 
         case "DeviceMessageRateExceed": 
          // Reduce the number of messages sent to this device 
          break; 

        } 
       } 
      } 
     } 
    } 
}