2013-04-05 75 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