【问题标题】:How to send ios silent push notification to Notification Hub with SendNotificationAsync method如何使用 SendNotificationAsync 方法向通知中心发送 ios 静默推送通知
【发布时间】: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


    【解决方案1】:

    原来我在将用户注册到 NotificationHub 时遇到问题。 "content-available":1 部分应该像下面的代码一样定义:

    string jsonBodyTemplate = "{\"aps\":{\"content-available":1, \"notificationtype\":\"$(notificationtype)\", \"extra\":\"$(extra)\"}}";
    

    【讨论】:

      猜你喜欢
      • 2019-07-21
      • 2018-02-17
      • 2018-12-05
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多