15

1 つのデバイスに複数の regid がある場合、GCM は正規 ID エラーを返します。

{"multicast_id":xxxx,"success":2,"failure":0,"canonical_ids":1,"results":[{"message_id":"xxxxx"},{"registration_id":"newest reg ID here","message_id":"xxxxxx"}]}

したがって、GCM で使用する必要がある最新の regid が表示されますが、削除する必要がある regid (古いもの) が表示されないのはなぜですか? 古い regid とデータベースから削除する必要があるものを確認するにはどうすればよいですか?

4

4 に答える 4

8

エランの答えは正しいですが、私にはまだ少し曇っています。しかし、彼のおかげで解決策を見つけました。

これがあなたの応答だとしましょう:

{
   "multicast_id":xxxxx,
   "success":7,
   "failure":0,
   "canonical_ids":2,
   "results":[
      {
         "message_id":"0:xxx%xxxxx"
      },
      {
         "message_id":"0:xxx%xxxxx"
      },
      {
         "registration_id":"MY_REG_ID_1",
         "message_id":"0:xxx%xxxxx"
      },
      {
         "message_id":"0:xxx%xxxxx"
      },
      {
         "message_id":"0:xxx%xxxxx"
      },
      {
         "registration_id":"MY_REG_ID_2",
         "message_id":"0:xxx%xxxxx"
      },
      {
         "message_id":"0:xxx%xxxxx"
      }
   ]
}

ご覧のとおり、7 つのメッセージのうち 2 つが重複しています。

これは、サーバーにメッセージを送信する方法です。

$tokenResult = mysql_query("SELECT reg_ids FROM table_with_regids"); //
$i = 0;
while($row = mysql_fetch_array($tokenResult)) {

     $registrationIDs[$i] = $row['reg_ids'];
     $i++;
}

エランの答えから:

リクエストを送信するたびに Google からレスポンスが返されるため、このレスポンスをトリガーしたリクエストで Google に送信された登録 ID を把握しておく必要があります。削除する必要がある古い登録 ID は、その要求の 2 番目の登録 ID です。

これは、配列$registrationIDs[]のインデックス25をMY_REG_ID_1MY_REG_ID_2に置き換える必要があることを意味します。

最後に double 値をチェックし、正確な重複を削除します。結果は、5 つの regid を持つ配列になります (または、MY_REG_ID_# で置き換えるのではなく、配列からそのインデックスを直接削除します)。

于 2013-04-12T13:25:43.310 に答える
2
<?php

//ASSUME gcm_registration_table field
// id || registration_id || user_id || created_at || updated_at


// FIND CANONICAL IDS POSITION
function CanonicalIdPosition($gcm_response)
{
    $c_ids = array();
    foreach ($gcm_response['results'] as $k => $val) {
        if (isset($val['registration_id'])) {
            $c_ids[] = $k;
        }
    }
    if ($c_ids) {
        return $c_ids;
    } else {
        return false;
    }
}

// Find Duplicate registration Ids from Server Matchind to index position
function DuplicateRegIdFromRegistrationTable($canonical_ids)
{

    DB::query("SELECT registration_id FROM gcm_registration_table");
    $results = DB::fetch_assoc_all();
    $duplicate_reg_val = array();

// Match Position and Find Value
    foreach ($results as $key => $val) {
        if (in_array($key, $canonical_ids)) {
            $duplicate_reg_val[] = $val['registration_id'];
        }
    }

    return $duplicate_reg_val;
}

// update existing Duplicate registration id with New Canonical registration ids
function UpdateDuplicateRegIds($duplicateVal)
{

    foreach ($duplicateVal as $val) {
        $sql = "UPDATE gcm_registration_table SET registration_id = {$val} WHERE registration_id ={$val}";
// Some Yours Code...
    }
}

// Method to send Notification to GCM Server
function SendGcmNotification($registatoin_ids_from_table, $message, $gcm_api_key, $dry_run = false)
{

// Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
        'dry_run' => $dry_run
    );

    $headers = array(
        'Authorization: key=' . $gcm_api_key,
        'Content-Type: application/json'
    );

//print_r($headers);
// Open connection
    if (!class_exists('curl_init')) {
        $ch = curl_init();
    } else {
        echo "Class Doesnot Exist";
        exit();
    }


// 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);

// Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
    $result = curl_exec($ch);

    if ($result === FALSE) {

        die('Curl failed: ' . curl_error($ch));
        return false;
    } else {
        return json_decode($result, true);
    }

// Close connection
    curl_close($ch);
}


//This Wont Send Notification to Device but gives you response to remove canonical ids
$gcm_response = SendGcmNotification($registatoin_ids_from_table, $message, $gcm_api_key, $dry_run = true);

$canonical_ids = CanonicalIdPosition($gcm_response);

if ($canonical_ids) {
    $duplicate_ids = DuplicateRegIdFromRegistrationTable($canonical_ids);
    UpdateDuplicateRegIds($duplicate_ids);
}

// Finally Get updated Registration Ids from table and send to GCM Server with
$gcm_response = SendGcmNotification($registatoin_ids_from_table, $message, $gcm_api_key, $dry_run = false);
于 2015-04-23T08:52:52.680 に答える