简体   繁体   中英

unicode characters are missing on sending gcm push notifications

I am sending push notifications by using gcm from my c# code. It is working fine when I send data in English. But when there is any unicode hindi character in data, it is missing from the json I get on android device. Please help me regarding this. My code is given below-

 public string SendNotification(string deviceId, string message)
    {

        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=unicode UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

        string postData = "delay_while_idle=1&data.data=" + value + "&registration_id=" + deviceId + "";
        Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;

        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = tRequest.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();

        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return sResponseFromServer;
    }

After analysing the code I get to know that It is the problem of encoding. Then by changing below Line in my code-

 string postData = "delay_while_idle=1&data.data=" + value + "&registration_id=" + deviceId + ""; 

to

 string postData = "delay_while_idle=1&data.data=" +  HttpUtility.UrlEncode(value) + "&registration_id=" + deviceId + "";

It is working fine. I am posting it here because I think It may help others also who have such type of problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM