我想用PHP脚本从数据库发送多个PN。在表中有3个设备令牌,但脚本只将它发送到第一个。
这里的剧本,有人有想法。
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'DEV.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
$ausgabe.= 'Connected to APNS' . PHP_EOL . '<hr>';
$body['aps'] = array(
'alert' => $message,
'sound' => 'note.wav',
'badge' => 1
);
$payload = json_encode($body);
$selectSQL = 'SELECT * from app_pushnotification';
$result = mysql_query($selectSQL);
while($row = mysql_fetch_array($result)){
$i+=1;
$ausgabe.= $i.' - '. $row['device_token'].' ';
$deviceToken = $row['device_token'];
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result){
$ausgabe.='Message to not delivered' . PHP_EOL . '<hr>';
} else {
$ausgabe.='Message to successfully delivered' . PHP_EOL . '<hr>';
}
}
$ausgabe.='close connection';
fclose($fp);
}
echo $ausgabe;
迎接凯文
答案 0 :(得分:2)
您的错误是您更改了循环中$result
的值。因此,脚本无法获取下一行数组。
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'DEV.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
$ausgabe.= 'Connected to APNS' . PHP_EOL . '<hr>';
$body['aps'] = array(
'alert' => $message,
'sound' => 'note.wav',
'badge' => 1
);
$payload = json_encode($body);
$selectSQL = 'SELECT * from app_pushnotification';
$result = mysql_query($selectSQL);
while($row = mysql_fetch_array($result)){
$i+=1;
$ausgabe.= $i.' - '. $row['device_token'].' ';
$deviceToken = $row['device_token'];
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$write_result = fwrite($fp, $msg, strlen($msg));
if (!$write_result){
$ausgabe.='Message to not delivered' . PHP_EOL . '<hr>';
} else {
$ausgabe.='Message to successfully delivered' . PHP_EOL . '<hr>';
}
}
$ausgabe.='close connection';
fclose($fp);
}
echo $ausgabe;
请注意,第二个$result =
现在是$write_result =
。
答案 1 :(得分:0)
您可以创建守护进程/ cronjob来发送推送通知。它将帮助您在不冻结UI的情况下发送异步和多条消息。参考链接:http://kvz.io/blog/2009/01/09/create-daemons-in-php/