【问题标题】:Sending push notification to multiple users android向多个用户发送推送通知android
【发布时间】:2013-05-13 12:41:20
【问题描述】:

我正在尝试向多个用户发送推送通知,当我为 1 个用户执行此操作时,它完美地工作,当我尝试将消息发送给多个用户时出现问题。

我收到了响应 200 MissingRegistration(所以我想问题是我没有以正确的方式发送 ID)

两个 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";

}

【问题讨论】:

    标签: php android push-notification google-cloud-messaging


    【解决方案1】:

    您使用的格式一次只能用于发送一个通知。 为了向多个注册 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)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多