the maximum characters of push notification is 256 bytes, when I try to send a message in arabic encoding the maximum length is less than 50 characters,
推送通知的最大字符为256字节,当我尝试以阿拉伯语编码发送消息时,最大长度小于50个字符,
I use this php file:
我使用这个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 reject the message above because is too longer, otherwise the maximum length of english characters is normal
APNS拒绝上面的信息,因为它太长,否则英语字符的最大长度是正常的。
what shall I do please!
我该怎么办呢?
1 个解决方案
#1
3
The PHP json_encode()
function uses Unicode escape sequences for the Arabic characters, so that the $payload
becomes:
PHP json_encode()函数对阿拉伯字符使用Unicode转义序列,因此$有效负载变为:
{"aps":{"alert":"\u0627\u0644 ... \u0644\u0627","sound":"default"}}
with a total length of 266 characters. This is valid (compare http://json.org), but makes the payload too long for APNS. Each Arabic characters uses 6 bytes instead of 2 UTF-8 bytes.
总长度为266个字符。这是有效的(比较http://json.org),但是使有效负载对APNS太长。每个阿拉伯字符使用6个字节而不是2个UTF-8字节。
According to https://*.com/a/10835469/1187415, you can use
根据https://*.com/a/10835469/1187415,您可以使用
$payload = json_encode($body, JSON_UNESCAPED_UNICODE);
in PHP 5.4.0 or later to turn off the Unicode escaping. I could not test this because my PHP version is older.
在PHP 5.4.0或更高版本中关闭Unicode转义。我不能测试它,因为我的PHP版本比较老。
The only other alternative would be not to use json_encode()
and create the JSON string "manually".
另一种选择是不使用json_encode()并“手动”创建JSON字符串。
Update: Apple has increased the maximum allowed payload size. From The Remote Notification Payload in the Local and Remote Notification Programming Guide:
更新:苹果增加了允许的最大有效载荷大小。来自本地和远程通知编程指南中的远程通知有效负载:
When using the HTTP/2 provider API, maximum payload size is 4096 bytes. Using the legacy binary interface, maximum payload size is 2048 bytes.
当使用HTTP/2提供程序API时,最大有效负载大小为4096字节。使用遗留二进制接口,最大有效负载大小为2048字节。
This makes JSON encoding of >300 Arabic characters possible with the "legacy interface" and even more with the new HTTP/2 based interface.
这使得“遗留接口”和新的基于HTTP/2的接口可以对>300个阿拉伯字符进行JSON编码。
#1
3
The PHP json_encode()
function uses Unicode escape sequences for the Arabic characters, so that the $payload
becomes:
PHP json_encode()函数对阿拉伯字符使用Unicode转义序列,因此$有效负载变为:
{"aps":{"alert":"\u0627\u0644 ... \u0644\u0627","sound":"default"}}
with a total length of 266 characters. This is valid (compare http://json.org), but makes the payload too long for APNS. Each Arabic characters uses 6 bytes instead of 2 UTF-8 bytes.
总长度为266个字符。这是有效的(比较http://json.org),但是使有效负载对APNS太长。每个阿拉伯字符使用6个字节而不是2个UTF-8字节。
According to https://*.com/a/10835469/1187415, you can use
根据https://*.com/a/10835469/1187415,您可以使用
$payload = json_encode($body, JSON_UNESCAPED_UNICODE);
in PHP 5.4.0 or later to turn off the Unicode escaping. I could not test this because my PHP version is older.
在PHP 5.4.0或更高版本中关闭Unicode转义。我不能测试它,因为我的PHP版本比较老。
The only other alternative would be not to use json_encode()
and create the JSON string "manually".
另一种选择是不使用json_encode()并“手动”创建JSON字符串。
Update: Apple has increased the maximum allowed payload size. From The Remote Notification Payload in the Local and Remote Notification Programming Guide:
更新:苹果增加了允许的最大有效载荷大小。来自本地和远程通知编程指南中的远程通知有效负载:
When using the HTTP/2 provider API, maximum payload size is 4096 bytes. Using the legacy binary interface, maximum payload size is 2048 bytes.
当使用HTTP/2提供程序API时,最大有效负载大小为4096字节。使用遗留二进制接口,最大有效负载大小为2048字节。
This makes JSON encoding of >300 Arabic characters possible with the "legacy interface" and even more with the new HTTP/2 based interface.
这使得“遗留接口”和新的基于HTTP/2的接口可以对>300个阿拉伯字符进行JSON编码。