Android gcm如何使用土耳其字符?

时间:2013-10-10 15:59:50

标签: android google-cloud-messaging

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

这样我从Android收到了一条消息。 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;
}

2 个答案:

答案 0 :(得分:0)

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

我想当你致电getBytes("8859_1")时,你会丢失土耳其字符。

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

答案 1 :(得分: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;
}