我有一个问题,我一次只能发送一条消息。这段代码工作得很好:
$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);
echo 'Connected to APNS' . PHP_EOL ."<br><br>";
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 1
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $token[1]) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo '<br><div style="color:red">Message not delivered' . PHP_EOL.'</div><br><br>';
else
echo '<br><div style="color:green">Message successfully delivered' . PHP_EOL.'</div><br><br>';
但是当我尝试连接消息(msg)时,我只得到它有效的消息 - 但什么也没有到达..
$ait = new ArrayIterator($token);
$cit = new CachingIterator($ait);
$msg2 = "";
foreach ($cit as $deviceToken) {
if($deviceToken != NULL && $message != NULL) {
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 1
);
$payload = json_encode($body);
$msg2 .= chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
}
else throw new Exception('SHOULD NOT BE NULL');
}
echo $msg2;
$result2 = fwrite($fp, $msg2, strlen($msg2));
fclose($fp);
有人可以帮忙吗?
编辑:忘记添加行 "$msg2 = "";" 在 foreach 循环前面。