0

使用手机发送的 GCM 消息,没有土耳其字符。下面的编码还没有提出解决方案。

这样我就收到了来自 Android 的消息。安卓代码

@Override
protected void onMessage(Context context, Intent intent) {
    String output = "";
    try {
        output = new String(intent.getStringExtra("message").getBytes("8859_1"), "utf-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    generateNotification(context, output);
}

ASP.NET C#以这种方式发送消息。另外,还有一个控制台应用程序。C# 代码

public string SendNotification(string deviceId, string message)
{
    var utf8 = Encoding.UTF8;
    byte[] utfBytes = utf8.GetBytes(message);
    message = utf8.GetString(utfBytes, 0, utfBytes.Length);   

    string GoogleAppID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    var SENDER_ID = "XXXXXXXXXXXXXXXXX";
    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=UTF-8";
    tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

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

    System.Web.HttpUtility.UrlEncode(message);

    string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" + 
    System.DateTime.Now.ToString() + "&registration_id=" + deviceId.ToString() + "";
    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;
}
4

2 回答 2

0

该代码解决了土耳其字符的问题。

public string SendNotification(string deviceId, string message)
{
    var utf8 = Encoding.UTF8;
    byte[] utfBytes = utf8.GetBytes(message);
    message = utf8.GetString(utfBytes, 0, utfBytes.Length);

    string GoogleAppID = "AIzaSyDlJwdWRKA-vO72ewwVcOxEklaBUwRybPI";
    var SENDER_ID = "1024375801813";
    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=UTF-8";
    tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

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

    System.Web.HttpUtility.UrlEncode(message);

    string postData = string.Format("registration_id={0}&data.message={1}&collapse_key={2}", deviceId.ToString(), HttpUtility.UrlEncode(message), HttpUtility.UrlEncode("score_update"));

    //string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" + 
    //System.DateTime.Now.ToString() + "&registration_id=" + deviceId.ToString() + "";
    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;
}
于 2013-12-04T08:59:55.630 回答
0

这条线似乎是错误的—— output = new String(intent.getStringExtra("message").getBytes("8859_1"), "utf-8");

我认为当您打电话时,getBytes("8859_1")您会丢失土耳其语字符。

尝试output = intent.getStringExtra("message");

于 2013-10-10T16:39:23.593 回答