2013-04-05 66 views
14

有1臺設備的多個regids當GCM返回規範ID錯誤: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是什麼以及我應該從我的數據庫中刪除哪一個?

+0

如何[Rü保持你的database.rü使用設備ID>? – 2013-04-05 11:05:33

+0

3列。一個用regid,另一個用app id(用於其他事項),第三個類型(ios或android) – 2013-04-05 11:06:34

+0

regid根據應用程序ID分配唯一。使用應用程序ID作爲你的槓桿。 – 2013-04-05 11:08:03

回答

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++; 
} 

從葉蘭的回答是:

Since you get a response from Google for each request you send, you should know which Registration IDs were sent to Google in the request that triggered this response. The old Registration ID that you have to delete is the second Registration ID in that request.

這意味着索引和5陣列 $ registrationIDs []應替換爲MY_REG_ID_1MY_REG_ID_2

最後檢查double值並刪除確切的重複項。結果應該是一個包含5個regid的數組(或直接從數組中刪除該索引,而不是用MY_REG_ID_#替換)。

4

您所包含的GCM響應表明您已向兩個註冊ID發送消息。這兩條消息都成功地在GCM服務中收到。只有第二條消息你有一個規範的註冊ID。

由於您收到了Google發送的每個請求的回覆,因此您應該知道在觸發此回覆的請求中哪些註冊ID已發送給Google。您必須刪除的舊註冊ID是該請求中的第二個註冊ID。

+0

我用一個例子添加了我自己的答案。儘管你的答案確實有幫助。謝謝。 – 2013-04-12 13:26:17

0
<?php 

// API access key from Google API's Console 

define('API_ACCESS_KEY', 'AIzaSyCa1vcyOF6UhM6cgvnwARBafmdl8haQo1Y'); 
$con=mysqli_connect("localhost","root","","bloodbank_master"); 
$response = array(); 

$q="SELECT `regester_id` FROM `gcm`"; 
$result1 = $con->query($q) ; 
if ($result1->num_rows > 0) { 

$response["users"] = array(); 

while ($row = $result1->fetch_array(MYSQLI_BOTH)) { 
    $user = array(); 
    $registrationIds = array($row[0]); 


    $msg = array 
    (
    'message' => 'hieee', 
    'title'  => 'Blood Bank', 
    'subtitle' => 'This is a subtitle. subtitle', 
    'tickerText' => 'Ticker text here...Ticker text here...Ticker text here', 
    'vibrate' => 1, 
    'sound'  => 1, 
    'largeIcon' => 'large_icon', 
    'smallIcon' => 'small_icon' 
    ); 

    $fields = array 
    (
    'registration_ids' => $registrationIds, 
    'data'   => $msg 
    ); 

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

    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
    curl_setopt($ch,CURLOPT_POST, true); 
    curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
    $result = curl_exec($ch); 
    curl_close($ch); 

    echo $result; 
    } 
} 
else { 
    $response["success"] = 0; 
    $response["message"] = "No users found"; 
} 
?> 
+3

嗨@Ranjit,只有代碼的答案通常是不被接受的。事實上,你的答案是否回答了這個問題? (如果你的答案有一些伴隨文字,我可能不需要問)... – 2015-03-20 13:58:12

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); 
+2

請詳細說明你的答案,因爲代碼只有答案不清楚。 – 2015-04-28 11:08:01

+0

按照步驟操作。這將是明確的,也功能方法解釋步驟 – Raghu 2015-05-07 18:07:45

+0

目前尚不清楚。人們沒有XX分鐘來檢查對他們來說毫無用處的答案 – Srneczek 2016-05-14 12:41:16