3

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,

I use this php file:

    <?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

what shall I do please!

Fatima
  • 464
  • 1
  • 8
  • 19

1 Answers1

3

The PHP json_encode() function uses Unicode escape sequences for the Arabic characters, so that the $payload becomes:

{"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.

According to https://stackoverflow.com/a/10835469/1187415, you can use

$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.

The only other alternative would be not to use json_encode() and create the JSON string "manually".


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.

This makes JSON encoding of >300 Arabic characters possible with the "legacy interface" and even more with the new HTTP/2 based interface.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    hey, I try it manually without json_encode() function, and it works `$payload = '{"aps":{"alert":"'.$message.'","sound":"default","badge":"+1"}}';` – Fatima Aug 15 '13 at 14:24
  • thank you, but the first one doesn't work maybe because my php version is older – Fatima Aug 15 '13 at 14:25
  • @Fatima: Yes, that is what is meant. But you have to check if `$message` contains special characters such as `"` or `\ `. – Martin R Aug 15 '13 at 14:27
  • what about older php version? – hasan Apr 02 '16 at 10:03
  • @hasan83: The problem does not really exist anymore since Apple has increased the maximum payload size. – Martin R Apr 02 '16 at 16:48