PHP and Java string length for Arabic text for iOS Push Notifications

We send iOS / Apple devices push notifications through PHP / Java firewalls.

The problem we are facing is that Java has different multibyte characters that PHP does.

For example, below is the Arabic text that we must send, one from the Java backend and one from the PHP backend system (PHP and the Java backend are both different, no relationship with each other):

يبدا بقرص العقيلي واللقيمات وينتهي مع خالد حرية بالامارات نكهة وبهار مع القصار-٦ مساءا على تلفزيون الكويت

Now when we check the length of the string:

 Java: length 106 byte 194 PHP: length 369 byte 547 

Apple now allows only 256 bytes payload to be used, so the Java backend can fully send the aforementioned Arabic text, while not allowed in PHP. We need to reduce the 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)); 

Does anyone ever face this problem? Any known solution for this?

Please inform.

+4
source share
2 answers

Got one trick [not sure if this is constant soln while] temporarily: may be useful to other people in the future

comment below line code

 //$payload = json_encode($body); 

And directly adding 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 WORKS as Charm !!

+5
source

Very interesting: -)

I really searched for such an amazing answer for a while, and you saved the day ... Probably, although the icons DO NOT work anymore !!! It works with me like this:

 $badge = 3; $payload = '{"aps":{"alert":"'.$message.'","sound":"'.$sound.'","badge":'.$badge.'}}'; 

I usually let the server side calculate the next icon counter.

0
source

Source: https://habr.com/ru/post/1492482/


All Articles