php-向多个用户发送推送通知
作者:互联网
我正在尝试向多个用户发送推送通知,当我为1个用户执行推送通知时,它可以正常工作,当我尝试将消息发送给多个用户时出现问题.
我收到响应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";
}
解决方法:
您使用的格式只能用于一次发送单个通知.
为了将通知发送到多个注册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).
标签:push-notification,google-cloud-messaging,php,android 来源: https://codeday.me/bug/20191123/2066615.html