【发布时间】:2019-10-01 13:25:16
【问题描述】:
我正在尝试使用 FCM API 在 android 应用程序上发送推送通知。如果我使用 Insomnia 或 Postman 之类的 HTTP 客户端,效果会很好。下面是画面吗:
和标题:
当然,我有一个注册 ID 和 FCM 服务器密钥,而不是 xxx。
但我需要从我的C# 代码中发送相同的内容。我总是有BadRequest。我不明白为什么。
这是我的代码:
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();
在我总是得到这个答案之前,我很伤心:
请帮帮我!
【问题讨论】:
-
你能添加一个像 fiddler 这样的代理来检查这两个有效载荷吗?这就是我要开始的地方。添加代理并检查 Headers、Body 等
-
谢谢。我会尝试。我以前从没用过。
-
错误请求几乎总是在您的有效负载的一个或多个字段上出现拼写错误。请记住,序列化将导致一对一的变量发送到字段。因此,请检查您的班级成员名称以确保它映射邮递员有效负载。
-
我检查了所有字段。我已经制作了 json 转储并将其放入
Insomniahttp 客户端。我对服务器身份验证密钥做了同样的事情。一切正常! -
然后您需要检查 Http 客户端创建的确切有效负载才能弄清楚这一点(提琴手之类的)。
标签: c# .net-core firebase-cloud-messaging dotnet-httpclient