2013-04-05 39 views
14

Khi có nhiều regids cho 1 thiết bị GCM trả về lỗi id kinh điển:GCM id kinh điển

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

Vì vậy, nó cho thấy regid mới nhất nên được sử dụng bởi GCM nhưng tại sao nó không được hiển thị regid mà bạn nên xóa (cái cũ)? Làm thế nào để tôi biết regid cũ là gì và tôi nên xóa cái nào từ cơ sở dữ liệu của mình?

+0

thế nào r u giữ database.r bạn u sử dụng thiết bị id>? –

+0

3 cột. Một với regid, một với id ứng dụng (được sử dụng cho các vấn đề khác), và thứ ba loại (ios hoặc android) –

+0

regid được gán duy nhất theo id ứng dụng. Sử dụng id ứng dụng làm đòn bẩy ur. –

Trả lời

8

Câu trả lời của Eran là chính xác, mặc dù tôi nhận thấy câu trả lời vẫn có chút sương mù đối với tôi. Tuy nhiên, nhờ anh ta tôi đã tìm ra một giải pháp.

nói đây là trả lời của bạn:

{ 
    "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" 
     } 
    ] 
} 

Như bạn có thể thấy 2 trong số 7 thông điệp là một trùng lặp.

Đây là cách tôi gửi tin nhắn đến máy chủ:

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

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

từ Eran của câu trả lờ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.

này có nghĩa là chỉ số và của mảng $ registrationIDs [] nên được thay thế bằng MY_REG_ID_1MY_REG_ID_2.

Cuối cùng, hãy kiểm tra giá trị gấp đôi và xóa các trùng lặp chính xác. Kết quả sẽ là một mảng với 5 regids (hoặc trực tiếp xóa chỉ mục đó khỏi mảng của bạn thay vì thay thế bằng MY_REG_ID_ #).

4

Phản hồi GCM mà bạn đưa vào cho biết rằng bạn đã gửi thư đến hai ID đăng ký. Cả hai tin nhắn đã được nhận trong dịch vụ GCM thành công. Chỉ cho thông báo thứ hai bạn có ID đăng ký chuẩn.

Vì bạn nhận được phản hồi từ Google cho mỗi yêu cầu bạn gửi, bạn nên biết ID đăng ký nào được gửi tới Google trong yêu cầu đã kích hoạt phản hồi này. ID đăng ký cũ mà bạn phải xóa là ID đăng ký thứ hai trong yêu cầu đó.

+0

Tôi đã thêm câu trả lời của riêng mình bằng ví dụ. Câu trả lời của bạn thực sự đã giúp. Cảm ơn. –

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

Xin chào @Ranjit, các câu trả lời chỉ viết mã thường bị cau mày. Trong thực tế, câu trả lời của bạn có trả lời câu hỏi không? (Nếu câu trả lời của bạn có một số văn bản đi kèm, tôi có thể không cần hỏi) ... –

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

Vui lòng giải thích câu trả lời của bạn vì mã chỉ câu trả lời không rõ ràng. –

+0

Làm theo các bước. Nó sẽ được rõ ràng và cũng có chức năng phương pháp giải thích các bước – Raghu

+0

Nó không phải là rõ ràng. Mọi người không có XX phút để kiểm tra câu trả lời có thể vô ích cho họ sau khi tất cả – Srneczek

Các vấn đề liên quan