C#FCM:远程服务器返回错误:(400)错误的请求

时间:2018-12-08 17:15:37

标签: c# firebase-cloud-messaging

在邮递员 WebAPI 中工作正常,但是当我尝试在代码中使用它时,它总是返回消息:

  

远程服务器返回错误:(400)错误的请求。

这是代码:

            var payload = new
            {
                to = deviceId,
                notification = new
                {
                    body = "Test",
                    title = "Test",
                },
                data = new
                {
                    message = "Hello, hows you?"
                }
            };

            var jsonBody = JsonConvert.SerializeObject(payload);
            using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send"))
            {
                httpRequest.Headers.TryAddWithoutValidation("Authorization", "key=" + applicationID);
                httpRequest.Headers.TryAddWithoutValidation("Sender", "id=" + SENDER_ID);
                httpRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");
                httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

                using (var httpClient = new HttpClient())
                {
                    var result = await httpClient.SendAsync(httpRequest);
                    //400 - bad Request
                }
            }

fyi(applicationId =服务器密钥)

1 个答案:

答案 0 :(得分:0)

如果FCM服务器上不存在deviceId,您将拥有(400) Bad Request

在您的FirebaseService onNewToken方法中,将令牌发送到服务器,并将其用于推送。