Send Arabic Text in Push Notification

We want to send Arabic Text in Push Notification. How can I send PHP or C# code?


Please advise.

Answered by Max108 in 35320022

As per this stackoverflow solution: stackoverflow.com/questions/2111076/push-notification-in-non-english-languages


For Arabic and some other non-English languages, you need to pass the length of the UTF encoded string before the counting.


Use...

writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
enter code herebyte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write((byte)b1.Length);    //payload length (big-endian second byte)


... to modify the following generic C# Apple Push Notification Provider:


int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";

//load certificate
string certificatePath = @"cert.p12";
string certificatePassword = "";
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(
        client.GetStream(),
        false,
        new RemoteCertificateValidationCallback(ValidateServerCertificate),
        null
);

try
{
    sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);
}
catch (AuthenticationException ex)
{
    client.Close();
    return;
}

// Encode a test message into a byte array.
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);

writer.Write((byte)0);  //The command
writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)

String deviceId = "DEVICEIDGOESHERE";
writer.Write(ToByteArray(deviceId.ToUpper()));

String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";

writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
writer.Write((byte)payload.Length); //payload length (big-endian second byte)

byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();

byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();

// Close the client connection.
client.Close();

There is a website dedicated to that difficulty I think...


EDIT: Nevermind; stackoverflow seems to be a better source for this.

Hello,


I visit the link but it is showing only the arabic text. Nothing is there related to push notification.


Please advise.

Something like this?


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

?>

In PHP 5.4.0 or later you can apparently turn off the Unicode escaping to allow for longer messages:

// Encode the payload as JSON
$payload = json_encode($body, JSON_UNESCAPED_UNICODE);


Otherwise you would need to create the JSON string as follows to avoid the character limitation caused by escaping:

// Encode the payload as JSON
$payload = '{"aps":{"alert":"'.$message.'","sound":"default","badge":"+1"}}';

Please can you give C# example as we are facing trouble with PHP to c# convertion? Because it is coming the unicode text as mesage not the arabic one.


I appreciate your help.


Thanks.

Accepted Answer

As per this stackoverflow solution: stackoverflow.com/questions/2111076/push-notification-in-non-english-languages


For Arabic and some other non-English languages, you need to pass the length of the UTF encoded string before the counting.


Use...

writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
enter code herebyte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write((byte)b1.Length);    //payload length (big-endian second byte)


... to modify the following generic C# Apple Push Notification Provider:


int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";

//load certificate
string certificatePath = @"cert.p12";
string certificatePassword = "";
X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(
        client.GetStream(),
        false,
        new RemoteCertificateValidationCallback(ValidateServerCertificate),
        null
);

try
{
    sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);
}
catch (AuthenticationException ex)
{
    client.Close();
    return;
}

// Encode a test message into a byte array.
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);

writer.Write((byte)0);  //The command
writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
writer.Write((byte)32); //The deviceId length (big-endian second byte)

String deviceId = "DEVICEIDGOESHERE";
writer.Write(ToByteArray(deviceId.ToUpper()));

String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";

writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
writer.Write((byte)payload.Length); //payload length (big-endian second byte)

byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();

byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();

// Close the client connection.
client.Close();

Hello, we tried as you mentioned but the problem is it is coming :-


if I encode then it is coming in the device like this "\u12312\u12123"


If I send arabic text it is coming like this :- ?????????


Please can you advise where is the issue?


Thanks

Have you had any luch on sending arabic text?

Even its the same ???? for me

Send Arabic Text in Push Notification
 
 
Q