Sinch Arabic Message

440 views Asked by At

I need to send SMS using Sinch API with Arabic text. My SMS message contains- English, Arabic and Numerical number.

What I tried

  1. I have read documentation for Sinch but they didn't provide any detailed documentation regarding how to use UTF-16BE and header encoding while sending this message.
  2. I also contacted their support team but they didn't either provided detailed solution about that. And whatever they mentioned didn't worked.

Below is the code that I have tried

String arabicFormattedMessage = "Some English Text \n\n"+"رجى الاتصال بالامتثال التجاري وحماية المستهلك (ككب) من دائرة التنمية الاقتصادية في 12345678 لمزيد من التفاصيل، إذا لم يتم الاتصال في غضون 5 أيام عمل.";
jsonObject.put("message", arabicFormattedMessage );

String messageBody = new String(jsonObject.toString().getBytes(),"UTF-16BE");
String contentTypeHeader = "application/json; charset=UTF-16BE";

headers.put("authorization", base64AuthHeader);
headers.put("content-type", contentTypeHeader);
headers.put("x-timestamp", timeStampHeader);

//headers.put("encoding", "UTF-16BE"); Tried with and without this, but still not working. 

Response I have recd

Sinch responseCode: 400
Sinch Response: message object is invalid. toNumber object is invalid.

As Requested in comments, Below is the working example with English Only:

 JSONObject jsonObject = new JSONObject();
 String formattedMessage = "some english message";
 jsonObject.put("message", formattedMessage );
 final String messageBody = jsonObject.toString();

 final String base64AuthHeader = "basic" + " " + base64AppDetails;
 String contentTypeHeader = "application/json; charset=UTF-8";
 final String timeStampHeader = DateUtil.getFormattedTimeString(new Date());

 headers.put("authorization", base64AuthHeader);
 headers.put("content-type", contentTypeHeader);
 headers.put("x-timestamp", timeStampHeader);

HTTP POST Request

 HttpClient.sendHttpPostRequest(url, messageBody, headers);

After successful call, I get message id in response from Sinch

1

There are 1 answers

4
lionscribe On BEST ANSWER

The problem is that whatever your string's internal encoding is (which in Java is always UTF-16), HttpClient.sendHttpPostRequest is probably sending it as UTF-8.
As DefaultHttpClient is Deprecated, we will do it the correct way using HttpURLConnection. You may want to see the HttpURLConnection Documentation on how it should be used regarding Authentication.

String arabicFormattedMessage = "Some English Text \n\n"+"رجى الاتصال بالامتثال التجاري وحماية المستهلك (ككب) من دائرة التنمية الاقتصادية في 12345678 لمزيد من التفاصيل، إذا لم يتم الاتصال في غضون 5 أيام عمل.";
jsonObject.put("message", arabicFormattedMessage );

URL url = new URL ("http://...");
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
try {
    urlConn.setDoInput (true);
    urlConn.setDoOutput (true);
    urlConn.setRequestProperty("Content-Type","application/json; charset=UTF-16");   
    urlConn.connect();  

    DataOutputStream out = new DataOutputStream(urlConn.getOutputStream ());
    out.writeBytes(URLEncoder.encode(jsonObject.toString(),"UTF-16"));
    out.flush ();
    out.close ();

    int responseCode = urlConnection.getResponseCode();
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    readStream(in);
    } finally {
       urlConnection.disconnect();
    }
}

If it doesn't work you should also try UTF-16BE, or UTF-16LE without byte order mark.