Android GCM缺少注册C#

时间:2015-03-28 20:57:00

标签: c# android push-notification google-cloud-messaging

我正在尝试使用Google GCM实施推送通知,但是,当我尝试向GCM发送通知时,我收到MissingRegistration错误。

到目前为止,我正在使用虚假的注册ID,但我认为在这种情况下会出现类似“未找到注册”的内容,对吗?

这是我的C#代码

    public static void send(List<String> clientRegistrationIds, GcmData data)
    {
        WebRequest request = WebRequest.Create(SEND_URL);

        // Headers.
        request.Method = POST;
        request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        request.Headers.Add(string.Format("Authorization: key={0}", API_KEY));
        request.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

        string postData = new JavaScriptSerializer().Serialize(new GcmRequest(clientRegistrationIds, data));
        Console.WriteLine(postData);

        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = request.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();
        Console.WriteLine(sResponseFromServer);

        tReader.Close();
        dataStream.Close();
        tResponse.Close();
    }

序列化的postData变量:

{"registration_ids":["123"],"data":{"message":"New request!"}}

我的回答:

Error=MissingRegistration

我做错了什么?

谢谢!

编辑:将内容类型更改为application/json。现在我收到了我期待的错误:InvalidRegistration。但是,application/x-www-form-urlencoded;charset=UTF-8不应该是正确的内容类型?

1 个答案:

答案 0 :(得分:1)

将ContentType更改为application/json并且它有效。