Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM unicode charcters are not received

This is my server side code or android side code. This code is working fine only for English messages. If I use Unicode charters like use Arabic language then it shows nothing in place of Arabic. In cause of English Arabic mix, it skip the only Arabic charters.

Kindly give me solution. Thanks!

This is my C# code

private string SendNotification(string authstring, string id, string msg)
    {
        try
        {
            ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true;
            WebRequest request = WebRequest.Create("https://android.googleapis.com/gcm/send");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

            request.Headers.Add(string.Format("Authorization: key={0}", authstring));
            string collaspeKey = Guid.NewGuid().ToString("n");
            string postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}", id, msg, collaspeKey);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

            return responseFromServer;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

And this is my Android side code that catch the message.

@Override
protected void onMessage(Context context, Intent intent) {        
    String message = ArabicUtilities.reshape(intent.getExtras().getString("payload"));
}
like image 346
Atif Mahmood Avatar asked Jul 16 '12 09:07

Atif Mahmood


3 Answers

André Oriani has the general idea for the fix. Even though the message is placed in the body of the request, it still needs to url encoded.

string postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}", id, msg, collaspeKey);

should be replaced with

string postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}", id, HttpUtility.UrlEncode(msg), HttpUtility.UrlEncode(collaspeKey));

You will need to add a reference to System.Web in order to use HttpUtility. See URL Encoding using C# for more information.

like image 65
Jon Avatar answered Nov 15 '22 21:11

Jon


Have you considered using base64 to encode the string sent through GCM? This way you would remove all encoding problems.

like image 24
skjelland Avatar answered Nov 15 '22 19:11

skjelland


Here's what solved it for me in Java:

On the server side:

encodedMessage = URLEncoder.encode(message, "UTF-8");

In the app:

decodedMessage = URLDecoder.decode(message, "UTF-8");
like image 42
Paul Beusterien Avatar answered Nov 15 '22 20:11

Paul Beusterien