【发布时间】:2020-07-14 20:11:27
【问题描述】:
我有一个可行的解决方案,可以将推送通知从后端发送到 NotificationHub,然后再发送到 Xamarin.Forms 应用程序。如果有效载荷包含“警报”,这可以正常工作。我想实现静默推送通知。 documentation 表示有效载荷需要包含 "content-available" : 1 并且没有警报、徽章或声音。您还需要添加到标题"apns-push-type" = "background" 和apns-priority 到标题。
预期结果: 我想使用 SendNotificationAsync 方法向 NotificationHub 发送静默推送通知。
实际结果: 我只能使用 SendAppleNativeNotificationAsync 发送它。
我尝试将内容可用添加到标题或将其添加到模板参数字典。您可以在这篇文章的最底部找到此代码。我还尝试以不同的方式注册有效负载结构:
{
"aps" : {
"content-available" : 1,
"acme1" : "bar",
"acme2" : 42
},
}
或
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
如果我使用 SendAppleNativeNotificationAsync 发送 pn 而不是使用 SendNotificationAsync 发送 pn,所有这些定义都适用于我的后端。
将设备注册到 NotificationHub
var hub = new SBNotificationHub("blah", "blah");
var deviceToken = GetToken();
string jsonBodyTemplate = "{\"aps\":{\"#(content_available)\":1, \"notificationtype\":\"$(notificationtype)\", \"extra\":\"$(extra)\"}}";
string expiryTemplate = DateTime.UtcNow.AddYears(10).ToString(CultureInfo.CreateSpecificCulture("en-US"));
var tags = new NSSet(categories.ToArray());
await hub.UnregisterAllAsync(deviceToken);
await hub.RegisterNativeAsync(deviceToken, tags);
await hub.RegisterTemplateAsync(deviceToken, IOS_TEMPLATE_NAME, jsonBodyTemplate, expiryTemplate, tags);
向 NotificationHub 发送推送通知
Dictionary<string, string> templateParameters = new Dictionary<string, string>();
templateParameters["notificationtype"] = "blah";
var headers = new Dictionary<string, string> { { "apns-push-type", "background" }, { "apns-priority", "5" }, { "content-available", "1" } };
var notification = new TemplateNotification(templateParameters);
notification.Headers = headers;
await hub.SendNotificationAsync(notification);
【问题讨论】:
标签: swift xamarin.forms push-notification apple-push-notifications azure-notificationhub