Reputation: 164
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)
Upvotes: 2
Views: 2546
Reputation: 1131
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.
Upvotes: 1