简体   繁体   中英

C# FCM : The remote server returned an error: (400) Bad Request

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM