android - 发送 gcm 消息的错误请求

标签 android google-cloud-messaging

我正在尝试通过 C# 发送 gcm 消息。

我尝试了几次,但在尝试以 json 方法发送时收到 http:400-Bad request

当我尝试以文本形式发送时,我无法阅读(rtl 语言)- 这就是我尝试使用 JSON 的原因。

谁知道是什么问题? 谢谢!

    private static string SendNotificationJson2(string id, string msg)
    {
            var AuthString = "AIzaSyDAtmaqSdutBQemqmd4dQgf33B_6ssbvXA";
            var RegistrationID = id;
            var Message = msg;


        //-- Create GCM request insted of C2DM Web Request Object --//
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");

        Request.Method = "POST";
        Request.KeepAlive = false;

        //-- Create Query String --//
        Dictionary<String, String> dict = new Dictionary<string, string>();
        dict.Add("registration_ids", RegistrationID);
        dict.Add("data", Message);
        dict.Add("collapse_key", "1");

        string postData = GetPostStringFrom(dict);
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        Request.ContentType = "application/json";
        Request.ContentLength = byteArray.Length;


        Request.Headers.Add("Authorization", "key=" + AuthString);

        //-- Delegate Modeling to Validate Server Certificate --//
        ServicePointManager.ServerCertificateValidationCallback += delegate(
                    object
                    sender,
                    System.Security.Cryptography.X509Certificates.X509Certificate
                    pCertificate,
                    System.Security.Cryptography.X509Certificates.X509Chain pChain,
                    System.Net.Security.SslPolicyErrors pSSLPolicyErrors)
        {
            return true;
        };

        //-- Create Stream to Write Byte Array --// 
        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        //-- Post a Message --//
        WebResponse Response = Request.GetResponse();
        HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
        if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
        {
            return "Unauthorized - need new token";

        }
        else if (!ResponseCode.Equals(HttpStatusCode.OK))
        {
            return "Response from web service isn't OK";
            //Console.WriteLine("Response from web service not OK :");
            //Console.WriteLine(((HttpWebResponse)Response).StatusDescription);
        }

        StreamReader Reader = new StreamReader(Response.GetResponseStream());
        string responseLine = Reader.ReadLine();
        Reader.Close();

        return "ok";
    }

    private static string GetPostStringFrom(Dictionary<string,string> postFieldNameValue)
    {
        // return Newtonsoft.Json.JsonConvert.SerializeObject(postFieldNameValue);
        return "\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";
    }</code>

最佳答案

你忘记了 Json 数据中的第一个括号

使用

return "{\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";

代替

return "\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";

关于android - 发送 gcm 消息的错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922070/

相关文章:

android - TextView 数组未从数组中正确填充

python - 从 django 服务器中存在的另一个 python 脚本引用当前打开的 XMPP 连接

android - 移动后端启动器连续查询永不返回

c# - 如何在 Xamarin Android 中使用谷歌云消息 (GCM)

java - 点击通知后如何正确地将新 Activity 添加到堆栈顶部

android - 将构建日期/时间添加到 Android 项目

android - 动画 View 作为连续页面

android - 在 bintray 上上传二进制文件时 HTTP/1.1 401 Unauthorized

python - GCM python 服务器端实现 : Error=MissingRegistration

c# - PushSharp :Android GCM Push Notification received without push message