发送 gcm 推送通知时缺少 unicode 字符
unicode characters are missing on sending gcm push notifications
我在我的 c# 代码中使用 gcm 发送推送通知。当我用英文发送数据时它工作正常。但是当数据中有任何 unicode 印地语字符时,它在 json 我在 android 设备上丢失。请帮我解决这个问题。我的代码如下-
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 + "®istration_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;
}
分析代码才知道是编码的问题。然后通过在我的代码中更改下面的行-
string postData = "delay_while_idle=1&data.data=" + value + "®istration_id=" + deviceId + "";
到
string postData = "delay_while_idle=1&data.data=" + HttpUtility.UrlEncode(value) + "®istration_id=" + deviceId + "";
它工作正常。我把它贴在这里是因为我认为它可以帮助其他有此类问题的人。
我在我的 c# 代码中使用 gcm 发送推送通知。当我用英文发送数据时它工作正常。但是当数据中有任何 unicode 印地语字符时,它在 json 我在 android 设备上丢失。请帮我解决这个问题。我的代码如下-
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 + "®istration_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;
}
分析代码才知道是编码的问题。然后通过在我的代码中更改下面的行-
string postData = "delay_while_idle=1&data.data=" + value + "®istration_id=" + deviceId + "";
到
string postData = "delay_while_idle=1&data.data=" + HttpUtility.UrlEncode(value) + "®istration_id=" + deviceId + "";
它工作正常。我把它贴在这里是因为我认为它可以帮助其他有此类问题的人。