没有收到Android的GCM单向code charcters [英] Android GCM unicode charcters are not received

查看:139
本文介绍了没有收到Android的GCM单向code charcters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的服务器端code或Android端code。 这code是工作的罚款仅为英文消息。如果我采用UNI code章程喜欢用阿拉伯语话,就说明没有到位阿拉伯语。 在英语阿拉伯语搭配的原因,它跳过唯一的阿拉伯语章程。

请给我的解决方案。 谢谢!

这是我的C#code

 私人字符串sendNotification的(字符串authstring,串号,串MSG)
    {
        尝试
        {
            ServicePointManager.ServerCertificateValidationCallback =(对象发件人,X509证书证书,X509Chain链,SslPolicyErrors sslPolicyErrors)=>真正;
            WebRequest的请求= WebRequest.Create(https://android.googleapis.com/gcm/send);
            request.Method =POST;
            request.ContentType =应用/的X WWW的形式urlen codeD;字符集= UTF-8;

            request.Headers.Add(的String.Format(授权:键= {0},authstring));
            字符串collaspeKey = Guid.NewGuid()的ToString(N)。
            字符串POSTDATA =的String.Format(registration_id = {0}&放大器; data.payload = {1}&安培; collapse_key = {2},身份证,味精,collaspeKey);
            字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);
            request.ContentLength = byteArray.Length;
            流式传输的数据流= request.GetRequestStream();
            dataStream.Write(字节数组,0,byteArray.Length);
            dataStream.Close();
            WebResponse的响应= request.GetResponse();
            数据流= response.GetResponseStream();
            StreamReader的读者=新的StreamReader(数据流);
            字符串responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

            返回responseFromServer;
        }
        赶上(例外前)
        {
            抛出前;
        }
    }
 

这是我的Andr​​oid端code,它截获该消息。

  @覆盖
保护无效的onMessage(上下文的背景下,意图意图){
    字符串消息= ArabicUtilities.reshape(intent.getExtras()的getString(有效载荷));
}
 

解决方案

安德烈Oriani酒店 具有修复的总体思路。即使邮件被放置在请求主体,它仍然需要给url EN codeD。

 字符串POSTDATA =的String.Format(registration_id = {0}&放大器; data.payload = {1}&安培; collapse_key = {2},身份证,味精,collaspeKey);
 

应替换为

 字符串POSTDATA =的String.Format(registration_id = {0}&放大器; data.payload = {1}&安培; collapse_key = {2},身份证,HttpUtility.UrlEn code(MSG),HttpUtility.UrlEn code(collaspeKey));
 

您将需要添加一个参考System.Web程序才能使用HttpUtility。使用C#了解更多信息,请参见 URL编码。

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"));
}

解决方案

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.

这篇关于没有收到Android的GCM单向code charcters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆