作为参数传递的消息是一个数组。但是没有发送推送,而如果我发送一个字符串,推送就会成功发送到IOS。
public function sendIosPush($registatoin_ids, $message) {
set_time_limit(0);
$this->logger->write("INFO :","message for IOS PUSH".$message);
header('content-type: text/html; charset: utf-8');
$passphrase = '******';
$deviceIds=$registatoin_ids;
$body['aps'] = array('alert' => $message);
//$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';
//$result = 'Start' . '<br />';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
foreach ($deviceIds as $item) {
//sleep(1);
$fp = stream_socket_client($this->iosServer, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
// Build the binary notification
$payload = json_encode($body);
$this->logger->write("INFO :","message for IOS PUSH".$payload);
$msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if ($fp) {
fclose($fp);
}
}
//set_time_limit(30);
}
答案 0 :(得分:0)
您可以使用JSON转移消息....
获取消息后,您可以解码JSON并将其解析为数组或任何您想要的东西。
如果您不想要JSON,也可以使用xml ...
在php中转换为json,你可以使用json_encdoe
函数。
<强>更新强> 这应该是你想要的:
public function sendIosPush($registatoin_ids, $message) {
if(!is_array($message))
$message=array($message);
set_time_limit(0);
$this->logger->write("INFO :","message for IOS PUSH".$message);
header('content-type: text/html; charset: utf-8');
$passphrase = '******';
$deviceIds=$registatoin_ids;
$body['aps'] = array(
'alert' => json_encode($message),
'sound'=>"default");
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
foreach ($deviceIds as $item) {
$fp = stream_socket_client($this->iosServer, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
// Build the binary notification
$payload = json_encode($body);
$this->logger->write("INFO :","message for IOS PUSH".$payload);
$msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if ($fp) {
fclose($fp);
}
}
//set_time_limit(30);
}
答案 1 :(得分:0)
function sendPushiOS($deviceToken, $pushMessage, $urlLink, $alertType)
{
$iosServer='ssl://gateway.sandbox.push.apple.com:2195';
$deviceTokens = $deviceToken;
$passphrase = '****';
$urlis = $urlLink;
$pushId = uniqid();
$result = htmlspecialchars_decode($pushMessage) . "\r\n" . htmlspecialchars_decode($urlis);
$message = $result;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$body['aps'] = array(
'alert' => $message,
'sound' => 'busy.wav',
'URL' => $urlis,
'pushMsgId' => $pushId
);
foreach ($deviceTokens as $udid)
{
$fp = stream_socket_client($iosServer, $err,$errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
$payload = json_encode($body);
if (!$fp)
{
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
$msg = chr(0) . pack('n', 32) . pack('H*', $udid) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if ($fp)
{
fclose($fp);
}
if (!$result)
{
echo "<br/>" . 'Message not delivered' . PHP_EOL;
}
}
}
这里$ deviceTokens是设备令牌数组。