向多个用户发送Android推送通知

3
我正在尝试向多个用户发送推送通知,当我仅向一个用户发送时,它可以完美地工作,但当我尝试向多个用户发送消息时出现问题。
我收到了响应200 MissingRegistration(所以我认为问题在于我没有以正确的方式发送ID)。
两个ID都是正确的,因为如果我单独向它们发送消息,它会起作用。
这是我的代码:
    $gcmcodes=array("one user key","the other user key");

    if(isset($_GET['mensaje']))    $message = $_GET['mensaje'];
    $message2 = new gcm(); 
    $message2->sendMessageToPhone(2, $message,$gcmcodes);

  function sendMessageToPhone($collapseKey, $messageText, $gcmcodes)
  {

$apiKey = 'My apikey';

$headers = array('Authorization:key=' . $apiKey);
$data = array(
  'registration_ids' => $gcmcodes,
  'collapse_key' => $collapseKey,
  'data.message' => $messageText);

$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers)
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<pre>";
var_dump($gcmcode,$response);
echo"</pre>";

if (curl_errno($ch)) {
 return 'fail';
}
if ($httpCode != 200) {
 return 'status code 200';
}
curl_close($ch);
return "mensaje mandado con exito";

}

1个回答

3
您正在使用的格式只能用于一次发送单个通知。 为了将通知发送到多个注册ID,您需要发送形式为JSON的请求:

{ "collapse_key": "something",
  "time_to_live": 108,
  "delay_while_idle": true,
  "data": {
    "message": "some message"
  },
  "registration_ids":["4", "8", "15", "16", "23", "42"]
}

你遇到 MissingRegistration 错误的原因是,当你使用纯文本格式时,GCM 期望单个注册 ID 成为 registration_id 键(而不是你尝试的 registration_ids)的值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接