Android GCM unicode字符未被接收

前端之家收集整理的这篇文章主要介绍了Android GCM unicode字符未被接收前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的服务器端代码Android代码.
代码仅适用于英文邮件.如果我使用Unicode字符,如使用阿拉伯语,那么它不会显示阿拉伯语.
在英语阿拉伯语混合的原因中,它跳过了唯一的阿拉伯语宪章.

请给我解决办法.
谢谢!

这是我的C#代码

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,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;
        }
    }

这是我的Android端代码,捕获消息.

@Override
protected void onMessage(Context context,Intent intent) {        
    String message = ArabicUtilities.reshape(intent.getExtras().getString("payload"));
}

解决方法

André Oriani
有一个修正的概念.即使消息被放置在请求的正文中,仍然需要对URL进行编码.
string postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}",collaspeKey);

应该换成

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

您将需要添加对System.Web的引用才能使用HttpUtility.有关更多信息,请参阅URL Encoding using C#.

猜你在找的Android相关文章