gpt4 book ai didi

c# - 使用 Azure 通知中心的带有应用程序数据的谷歌云消息传递负载

转载 作者:行者123 更新时间:2023-11-30 15:56:25 25 4
gpt4 key购买 nike

我正在尝试从我的后端应用程序向 Android 手机发送通知。我设法安装并删除了设备。现在我在消息有效负载方面遇到了问题。我需要声音提示,我需要在消息中发送一些应用程序数据。这就是我现在构建有效载荷的方式,但我认为这并不好:

string notificationText = NotificationText(story, profile);

JProperty messageJProperty = new JProperty("message", notificationText);
JObject messageJObject = new JObject(messageJProperty);
JProperty objectJProperty = new JProperty("data", messageJObject);
JObject message = new JObject(objectJProperty);
var payload = message.ToString();

return payload;

谢谢

更新(2017 年 11 月 3 日):我发现 Azure 会接受这种负载格式:

private string Payload(string notificationText, StoryEntity story, ProfileEntity profile, string deviceToken)
{
var payload = new JObject
(
new JProperty("registration_ids", new JArray(deviceToken)),
new JProperty("data", new JObject(
new JProperty("title", "Mapporia has new stroy>"),
new JProperty("message", notificationText)
)),
new JProperty("notId", $"{new Random().Next(int.MaxValue)}"),
new JProperty("content-available", 1),
new JProperty("soundname", "default"),
new JProperty("image", @"www/assets/img/logo.png"),
new JProperty("image-type", "circle"),
new JProperty("style", "inbox"),
new JProperty("notData", new JObject(
new JProperty("storyId", story.Id),
new JProperty("profileId", profile.Id)
))
).ToString(Newtonsoft.Json.Formatting.None);

return payload;
}

这是我的 json 的样子:

enter image description here

但现在 Azure 抛出异常:

1 2017-11-01 Create Story : The remote server returned an error: (400) Bad Request. The supplied notification payload is invalid.TrackingId:666febf6-85fe-4ebd-867d-00ce5a668809_G3,TimeStamp:11/1/2017 9:53:07 PM

我错过了什么吗?根据这个page ,我建错了!

最佳答案

GCM 的正确 JSON 格式是:

    {
"to" : "{{devicetoken}} OR {{registrationID form Azure}}",
"data":
{
"title":"{{title goes here}}",
"message":"{{message body goes here}}",
"priority":"high"
},
"notId":"{{unique ID, I used RANDOM to generate it}}",
"content-available":1,
"soundname":"default",
"image":"www/assets/img/logo.png",
"image-type":"circle",
"style":"inbox",
"notData":
{
"storyId":1,
"profileId":6
}
}

以及如何使用 C# 和 Newtonsoft JSON nuget packege 构建此 JSON:

            var payload = new JObject
(
new JProperty("to", deviceToken),
new JProperty("data", new JObject(
new JProperty("title", "title goes here"),
new JProperty("message", "notification text goes here"),
new JProperty("priority", "high")
)),
new JProperty("notId", $"{new Random().Next(int.MaxValue)}"),
new JProperty("content-available", 1),
new JProperty("soundname", "default"),
new JProperty("image", @"www/assets/img/logo.png"),
new JProperty("image-type", "circle"),
new JProperty("style", "inbox"),
new JProperty("notData", new JObject(
new JProperty("storyId", story.Id),
new JProperty("profileId", profile.Id)
))
).ToString(Newtonsoft.Json.Formatting.None);

关于c# - 使用 Azure 通知中心的带有应用程序数据的谷歌云消息传递负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com