1

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

Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33
  • Did you try encoding it into utf-8 – lionscribe Sep 03 '17 at 05:40
  • Yes, I tried, it didn't worked and even support team of sinch asked me to parse the message in UTF-16BE. Also, from the documentation too you can find below line: "characters in languages such as Arabic, Chinese, Korean, Japanese, or Cyrillic alphabet languages (e.g., Ukrainian, Serbian, Bulgarian, etc) must be encoded using the 16-bit UCS–2 character encoding. " Reference- https://www.sinch.com/docs/sms/ – Avtar Guleria Sep 03 '17 at 06:54
  • It should really be sent as utf8 – cjensen Sep 05 '17 at 20:48
  • @cjensen, I tried with UTF-8 too, and received correct response with messageID, but still when I receive message on my phone number arabic characters are replaced by ????. – Avtar Guleria Sep 06 '17 at 09:19
  • @cjensen: UTF-8 works, Also, for Android I have to use HttpURLConnection instead of DefaultHttpClient API. – Avtar Guleria Sep 06 '17 at 09:46

1 Answers1

1

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.

lionscribe
  • 3,413
  • 1
  • 16
  • 21
  • I am doing this, will it not work-String messageBody = new String(jsonObject.toString().getBytes(),"UTF-16BE"); – Avtar Guleria Sep 04 '17 at 06:38
  • @AvtarGuleria Can you post code that works at least when there is no Arabic in the text? Update your question with a working example. That way we can try to get this working step by step. – lionscribe Sep 04 '17 at 12:21
  • @AvtarGuleria I have just updated the Answer. Please check. – lionscribe Sep 05 '17 at 16:18
  • Thanks for your input finally code works, but it works with combination of your and "cjensen" suggestions. I tried changing DefaultHttpClient with HttpURLConnection as per your suggestion. And used "UTF-8" as per cjensen suggestion. – Avtar Guleria Sep 06 '17 at 09:39