推送通知消息的最大长度(阿拉伯字符)ios



推送通知的最大字符数为256字节,当我尝试用阿拉伯语编码发送消息时,最大长度小于50个字符,

我使用这个php文件:

    <?php
// Put your device token here (without spaces):
$deviceToken = '2ca0c25ed7acea73e19c9d9193e57a12c1817ed48ddf8f44baea42e68a51563c';
// Put your private key's passphrase here:
$passphrase = 'pushp12';
// Put your alert message here:
$message = 'الاشعار الاول للتطبيق مرحبا بكم واهلا وسهلا';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$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;
// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>

APNS拒绝上面的消息,因为太长了,否则英文字符的最大长度是正常的

我该怎么办!

PHP json_encode()函数使用Unicode转义序列来替换阿拉伯字符,使得CCD_ 2变为:

{"aps":{"alert":"u0627u0644 ... u0644u0627","sound":"default"}}

总长度为266个字符。此有效的(比较http://json.org),但是使得有效载荷对于APNS来说太长。每个阿拉伯字符使用6个字节而不是2个UTF-8字节。

根据https://stackoverflow.com/a/10835469/1187415,您可以使用

$payload = json_encode($body, JSON_UNESCAPED_UNICODE);

在PHP 5.4.0或更高版本中关闭Unicode转义。我无法对此进行测试,因为我的PHP版本比较旧。

唯一的其他选择是不使用json_encode()并创建JSON字符串"手动"。


更新:苹果增加了允许的最大有效负载大小。来自远程通知有效负载在本地和远程通知编程指南中:

当使用HTTP/2提供程序API时,最大有效负载大小为4096字节。使用传统二进制接口,最大有效载荷大小为2048字节。

这使得使用"遗留接口",新的基于HTTP/2的接口更是如此。

最新更新