使用手机发送的 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() + "®istration_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;
}