2

In postman WebAPI is working fine but when i tried to use it in code it always return message :

The remote server returned an error: (400) Bad Request.

here is the code :

            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 = serverkey)

  • var payload = new { to = deviceId, notification = new { body = "Test", title = "Test", }, data = new { message = "Hello, hows you?" } }; var jsonBody = JsonConvert.SerializeObject(payload); – Dhaval Thakkar Dec 08 '18 at 17:16
  • I think the error is in the payload, can you put it from the comment in the question and formatted correctly? can you also dump the `result` HttpResponseMessage completely? – Falco Alexander Dec 08 '18 at 18:09
  • same payload tried with postman and it works. – Dhaval Thakkar Dec 09 '18 at 05:58
  • can you put it from the comment in the question and formatted correctly? can you also dump the result HttpResponseMessage completely? – Falco Alexander Dec 09 '18 at 07:36
  • question updated. and SendAsync throws an exception : An error occurred while sending the request., Inner Exception : The remote server returned an error: (400) Bad Request.,Status : Protocol Error – Dhaval Thakkar Dec 09 '18 at 08:04
  • can you explore the result object a bit deeper? usually you'll find some hints why the request was considered bad. – Falco Alexander Dec 09 '18 at 08:29
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184952/discussion-between-dhaval-thakkar-and-falco-alexander). – Dhaval Thakkar Dec 09 '18 at 09:07
  • The `SenderId` is not necessary to complete a Downstream Message request, just the `Authorization` which must have your Server Key. If that still doesn't work, could you try sending the request [using Postman](https://stackoverflow.com/a/45310143/4625829) and see if it still throws an error? – AL. Dec 12 '18 at 06:56
  • have you resolved your issue? I am getting same error – A.s.ALI Oct 08 '19 at 05:07

1 Answers1

1

If not exist deviceId on FCM server you will have (400) Bad Request.

In your FirebaseService onNewToken method, send token to your server and use it for push.

AmirNorouzpour
  • 1,119
  • 1
  • 11
  • 26