2013-05-13 53 views
3

我试图向多个用户发送推送通知,当我为1个用户完成工作时,问题就出现在我尝试将消息发送给多个用户时。发送推送通知给多个用户android

我得到的回应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"; 

}

回答

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)。