提前感谢那些会提供帮助的人
当我向单个设备发送推送通知时,我使用这种方法
<?php
$deviceToken = $_POST['TOKEN'];
$message = $_POST['MESSAGGIO'];
$badge = 0;
$sound = "default";
$body = array();
$body['aps'] = array("alert" => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
$ctx = stream_context_create();
stream_context_set_option($ctx, "ssl", "local_cert", "apns-dev.pem");
$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
echo json_encode("errore");
return;
}
$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(" ", "", $deviceToken)) . pack("n",strlen($payload)) . $payload;
fwrite($fp, $msg);
fclose($fp);
?>
而当我必须向数据库中的所有设备发送通知时(当然使用设备令牌),我会考虑这样做
<?php
$deviceToken = $_POST['TOKEN'];
$message = $_POST['MESSAGGIO'];
$badge = 0;
$sound = "default";
$body = array();
$body['aps'] = array("alert" => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;
$ctx = stream_context_create();
stream_context_set_option($ctx, "ssl", "local_cert", "apns-dev.pem");
$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
echo json_encode("errore");
return;
}
$payload = json_encode($body);
//****
$db = new PDO('sqlite:my_db.sqlite');
$query = "SELECT device_token FROM user";
$result = $db->query($query);
foreach($result as $row){
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(" ", "", $row['device_token'])) . pack("n",strlen($payload)) . $payload;
fwrite($fp, $msg);
}
//****
fclose($fp);
?>
根据您的说法是正确的解决方案吗?