【发布时间】:2012-06-08 12:04:30
【问题描述】:
我的 PHP 代码有点问题。 我没有让它工作,我发送的消息将在所有设备上收到,女巫保存在我的数据库中。
设备上的推送应用程序会在我的 MySQL 数据库中自动注册 deviceToken。一切正常。
最大的问题是,我只从数据库中将推送消息发送到第一个设备。另一台设备没有收到任何消息。
这是我的代码:
<?php
require_once('konfiguration.php');
$sql = "SELECT deviceToken FROM DeviceTokens";
$dbquery = mysql_query($sql);
// check checkboxes
$message = $_POST['alert'];
$badge = $_POST['badge'];
$sound = $_POST['sound'];
// Construct the notification payload
$body = array();
// anti-error variable
$nothing = true;
// get user input
if ($message) { $alert=$_POST['message']; $nothing = false;
$body['aps']['alert'] = $alert; }
if ($badge) { $nothing = false;
if($_POST['number']) $number=(int)$_POST['number'];
else $number=1;
$body['aps']['badge'] = $number; }
if ($sound) { $nothing = false;
$body['aps']['sound'] = 'beep.wav'; }
// if input not correct, scream
if ($nothing || ($message && $alert=="")) { echo "Falscher Input!"; exit(); }
//make new stream context
$ctx = stream_context_create();
// set parameters
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'w3workProd.pem');
//stream_context_set_option($streamContext, 'ssl', 'passphrase', 'nils');
$socketClient = stream_socket_client('ssl://gateway.push.apple.com:2195', $error,
$errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload = json_encode($body);
print "sending message :" . $payload . "\n";
while($row = mysql_fetch_array($dbquery)) {
$message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken))
.chr(0) . chr(strlen($payload)) . $payload;
$deviceToken = str_replace(' ', '', $row['deviceToken']);
$message = pack('CnH*', 0, 32, $deviceToken);
$message = $message . pack('n', strlen($payload));
$message = $message . $payload;
fwrite($socketClient, $message);
echo ($row['deviceToken']);
}
fclose($socketClient);
exit();
?>
如果你能帮忙就太好了。 为什么它适用于一台设备...而不适用于其他设备...
【问题讨论】:
-
只有接收消息的设备是发送消息的设备吗?
-
只有 Device 获得推送,巫婆 deviceToken 是 Databse 中的第一个(取决于 ID 号)。其他人没有收到任何消息。我通过一个 html 网站的小界面发送了消息。一切正常,但只有第一个令牌被用于消息。其他的没有收到任何东西
-
删除所有
exit然后尝试。也许其中一个退出脚本(如果在循环中使用)。 -
我删除了“exit();”但仍然无法正常工作...推送消息正在第一个设备上接收,其他 2 个设备没有收到消息。
-
在循环中打开和关闭套接字。这是我猜测为什么你的脚本没有达到预期效果的最后希望。
标签: php ios push-notification push apple-push-notifications