I am trying to use FCM API for sending push notifications on a android application. It works nice if I use some HTTP client like Insomnia or Postman. Is the screen below:
and headers:
Of course, I have a registration ID and FCM Server key instead of xxx
.
But I need to send the same from my C#
code. And I always have BadRequest
. I don't understand why.
Here is my code:
var json = JsonConvert.SerializeObject(new
{
to = customer.FcmRegistrationId,
notification = new
{
body = push.PushBody,
title = push.PushTitle,
sound = "default"
}
});
var content = new StringContent(json, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
{
RequestUri = new Uri("https://fcm.googleapis.com/fcm/send"),
Content = content,
};
request.Headers.TryAddWithoutValidation("Authorization",
"key=" + customer.FCMServerKey);
HttpResponseMessage response;
using (var client = new HttpClient())
{
response = await client.SendAsync(request);
}
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
And as I've sad before I always have this answer:
Please help me!