我已经制作了一个代码,可以在许多保存在 db 上的设备上发送推送通知(用于分发),我已经制作了 php:
// Put your alert message here:
$message = "send push";
// Put your private key's passphrase here:
$passphrase = 'phrase';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS'. "<br>" ;
ob_flush();
flush();
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
if (mysql_num_rows($results)!=0) {
while($row = mysql_fetch_array($results))
{
$deviceToken= $row['deviceToken'];
if(isset($deviceToken)&&(strcmp($deviceToken,"(null)")!=0)){
echo "<br>".$deviceToken."<br>";
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered';
else
echo 'Message successfully delivered' ;
ob_flush();
flush();
}
}
// Close the connection to the server
fclose($fp);
}
如果我在 db 上有,只有 deviceToken 正确或具有值(null),则发送消息,但如果我有一个 deviceToken 错误,我会收到消息“消息已成功传递”,该消息已发送所有消息但不起作用。我在哪里犯了错误?谢谢。