对于IOS push如何发送数组消息
Posted
技术标签:
【中文标题】对于IOS push如何发送数组消息【英文标题】:For IOS push how to send an array message 【发布时间】:2015-10-16 19:03:15 【问题描述】:作为参数传递的消息是一个数组。但是推送没有被发送,而如果我发送一个字符串,则推送成功发送到 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);
【问题讨论】:
【参考方案1】:您可以使用 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);
【讨论】:
我所做的与您所说的类似。但它不起作用。你能帮我举个例子吗【参考方案2】: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 是设备令牌数组。
【讨论】:
以上是关于对于IOS push如何发送数组消息的主要内容,如果未能解决你的问题,请参考以下文章
从 raix:push 向 0 个 ios 应用程序 0 个 android 应用程序发送消息
无法将iOS CriticalAlert发送到Firebase云消息传递