我想將GCM消息發送到多個註冊設備。當僅發送給一個(作爲測試)時,它可以工作,但是當添加其餘的ID時,我會收到無效的註冊錯誤並且不會發送任何內容。GCM - 一個註冊ID正在工作,但因多個註冊ID失敗
我做到以下幾點:
$sql = "select group_concat(gcm_id) from users where gcm_id is not null";
如果只有1 ID(regid1),它的工作原理,如果有一個以上(regid1,regid2),它失敗invalidregistration。我試過如下:
$sql = "select group_concat(concat('\"',gcm_id,'\"')) from users where gcm_id is not null";
失敗兩個1號( 「regid1」)和幾個ID( 「gcmid1」, 「gcmid2」)。
regid的格式應該如何工作?
$sql = "select group_concat(gcm_id) from users where gcm_id is not null";
if ($stmt = $con->prepare($sql)) {
if ($stmt->execute()) {
$stmt->bind_result($gcm_ids);
$stmt->fetch();
$ids = array($gcm_ids);
$stmt->close();
} else trigger_error("Problem retrieving gcm ids");
} else trigger_error("Problem retrieving gcm ids");
$con->close();
if (empty($gcm_ids)) trigger_error("no registrations");
else sendGoogleCloudMessage( $data, $ids);
function sendGoogleCloudMessage($data, $gids) {
$apiKey = '...';
$url = 'https://android.googleapis.com/gcm/send';
$post = array('registration_ids' => $gids,'data' => $data);
$headers = array('Authorization: key='.$apiKey,'Content-Type: application/json');
$ch = curl_init();
// Set URL to GCM endpoint
curl_setopt($ch, CURLOPT_URL, $url);
// Set request method to POST
curl_setopt($ch, CURLOPT_POST, true);
// Set our custom headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get the response back as string instead of printing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set post data as JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
// Actually send the push!
$result = curl_exec($ch);
if (curl_errno($ch)) trigger_error('Error: '.curl_error($ch));
else echo "sent: ".$result;
curl_close($ch);
}
後您發送GCM – Pavya 2015-04-04 11:10:39
我更新了所有代碼我的帖子 – Amos 2015-04-04 11:24:47