PHP & Java string length Arabic text size issue for iOS Push Notifications

apple-push-notifications, character-encoding, java, php, string

Solution

Got one trick [not sure this is permanent soln though] temporarily: May be handy to other people in future

commented below code of line

//$payload = json_encode($body);

And directly appending `JSON format to Payload variable:`

// static sound and badge commands
$payload = '{"aps":{"alert":"'.$message.'","sound":"default","badge":"+1"}}';

// dynamic sound and badge commands
$payload = '{"aps":{"alert":"'.$message.'","sound":"'.$sound.'","badge":'.$badge.'}}';

Here, `$message` will be my `Arabic string` and it `WORKS` like a Charm !!

Problem

We are sending iOS / Apple devices push notifications via PHP / Java backend systems. The issue now we're facing is, Java has different multibyte character count that PHP does. For example, below is the Arabic text that we have to send, one from Java backend and one from PHP based backend system (PHP & Java backend - both are different, no relations with each other): `يبدا بقرص العقيلي واللقيمات وينتهي مع خالد حرية بالامارات نكهة وبهار مع القصار-٦ مساءا على تلفزيون الكويت` Now, when we check the string length: ``` Java: length 106 byte 194 PHP: length 369 byte 547 ``` Now Apple allows only `256 bytes` of Payload, hence Java backend is able to send above mentioned Arabic text in full, while same is not being allowed in PHP. We needs to reduce text for PHP. Below is my PHP Code: ``` // PHP Interpretation: echo $str = 'يبدا بقرص العقيلي واللقيمات وينتهي مع خالد حرية بالامارات نكهة وبهار مع القصار-٦ مساءا على تلفزيون الكويت'; echo '<br><br>'; $payload['aps'] = array('alert' => $str, 'sound' => 'default'); $payload = json_encode($payload); echo $payload; echo strlen($payload); echo '<br><br>'; echo '<br><br>'; echo utf8_encode($str); echo '<br><br>'; echo json_encode($str); echo '<br><br>'; echo strlen(json_encode($str)); echo '<br>'; echo strlen(utf8_encode($str)); echo '<br>'; echo mb_strlen(json_encode($str)); ``` Have anybody face this issue ever? Any known solution for this? Please advise.

Original source