如何将iPhone推送通知发送到多个设备。

时间:2015-03-13 06:16:58

标签: php iphone push-notification apple-push-notifications

我从数据库获取设备令牌,但是成功发送的通知,它只是接收到数据库中的最新设备。

但我的数据库中有3个设备令牌。

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'badge' => 1,
    'sound' => 'default'
    );
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$result = mysql_query("SELECT DeviceToken FROM iOS");
while($row = mysql_fetch_array($result))
{
    $deviceToken=$row['DeviceToken'];   
    echo $deviceToken;
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
}
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered '.PHP_EOL;
// Close the connection to the server
fclose($fp);
echo "<script>alert('Notification Sent Successfuly!'); location.href='Pushnotification.php';</script>";

1 个答案:

答案 0 :(得分:0)

将此行放入while循环

$result = fwrite($fp, $msg, strlen($msg));