我不确定如何向多个令牌发送推送通知。使用我的脚本似乎只向一个发送推送通知...根据Apple,最好有一个打开的连接并保持打开并在需要时发送消息。不知道怎么在我的php服务器上做...
$payload['aps'] = array('alert' => "New Cave report for ".$caveName,'badge' => 1, 'sound' => 'default');
$payload['condition'] = array('conditionID' => $ccID, 'caveName' => $caveName);
$payload = json_encode($payload);
// Connection Part
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = '/var/www/web543/files/apns-dev.pem';
if (!file_exists($apnsCert) )
{
echo "Certification file not found!";
} else
{
$streamContext = stream_context_create();
stream_context_set_option($streamContext,'ssl','local_cert',$apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort,$error,$errorString,2,STREAM_CLIENT_CONNECT,$streamContext);
if ( !$apns )
{
echo "Connection Failed!".$errorString;
} else
{
$db->query("SELECT DISTINCT token FROM notifications WHERE userID != '{$userID}'");
if ($db->num_rows()>0)
{
while ($db->next_record())
{
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $db->f('token'))) . chr(0) . chr(strlen($payload)) . $payload;
if ( fwrite($apns, $apnsMessage) === FALSE )
{
echo "Can not write";
}
}
}
}
fclose($apns);
}
$ apnsMessage的一些输出
Token: 5a984922e19eab54f78fd54e24d5b02a3d30ccdbbeee34aadbdacaa687ee1261 Message: Z˜I"áž«T÷ÕN$Õ°*=0ÌÛ¾î4ªÛÚʦ‡îa†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: c607acc70bd4885bf56f3b4827523023bf93a1d644626768ab0304bb3b4414dc Message: ƬÇÔˆ[õo;H'R0#¿“¡ÖDbgh«»;D܆{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: 785ec3128972bd3d4c3e6fa1eeead97b73b0696e2361339a2467e6ba775b83ea Message: x^Ér½=L>o¡îêÙ{s°in#a3š$gæºw[ƒê†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: c592487e3c71e921d0b7a825b66ed5e58070fee709131535ac391f14febbcfdc Message: Å’H~Token: 061bc20ba3a0fc17c689e052b42b5789f502a52d43180ea114e3212077045315 Message: £ üƉàR´+W‰õ¥-C¡ã! wS†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: 26fb66fef67a122ca456f106363115285d4d7156e7c8ab6e51bd5bfa9bab2d03 Message: &ûfþöz,¤Vñ61(]MqVçÈ«nQ½[ú›«-†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
答案 0 :(得分:1)
确定。我发现了问题。
当编译为Debug时,它将仅在使用DEV认证时在第一个设备上发送。如果我像Ad-Hoc一样使用部署,我必须使用Prod服务器和产品证书。
现在一切正常。
答案 1 :(得分:0)
我认为你必须根据令牌的数量在循环中运行这个脚本。
表示首先获取令牌数组,然后根据计数进行循环。然后从索引0中获取一个设备令牌并运行此脚本。然后在索引1处选择另一个令牌,然后再次运行此脚本。
要向多台设备发送推送通知,您每次都要连接到Apple服务器并关闭连接,然后再次打开并发送。这是此过程。
答案 2 :(得分:0)
是的,每次需要发送推送时,都不应该制作和拆除套接字连接。建立套接字连接后,您可以将所需的推送消息写入任意数量的设备。 您正在观察的确切问题是什么?您的套接字连接是否被删除?
如果您需要其他帮助,请查看此问题:How to send Push Notification to multiple devices?