iPhone 正在启动时发送推送通知 - 通知未发送到设备

Sending a push notification while iPhone is starting up - notification not delivered to device

我们收到来自测试人员的报告,称并非所有 Apple 推送通知都已发送。具体来说,如果在目标设备启动时发送通知(启动、建立网络连接等),则永远不会收到通知.

我了解 APNS 会尽最大努力发送通知。我仔细阅读了 Apple's documentation,其中说明了以下关于 QoS 的内容:

Apple Push Notification service includes a Quality of Service (QoS) component that performs a store-and-forward function. If APNs attempts to deliver a notification and the destination device is offline, APNs stores the notification for a limited period of time and delivers it when the device becomes available again. This component stores only the most recent notification per device and per app. If a device is offline, sending a notification request targeting that device causes the previous request to be discarded. If a device remains offline for a long time, all its stored notifications in APNs are discarded.

我已将 apns-expiration 设置为 90 分钟以使用 QoS 功能。

通知 发送时间:

顺便说一句,我们正在使用 Azure 通知中心的模板通知来与 APNS 集成。因此,我们的 APNS 过期和优先级配置如下所示:

registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, PushNotifications.IosTemplate)
{
    Expiry = DateTimeOffset.UtcNow.AddMinutes(90).ToString("o"),
    Priority = "10"
};

我怎么debug/troubleshoot这个问题,有没有人遇到过类似的行为?

通过向每个模板通知添加以下 headers 解决了此行为:

var templateProperties = new Dictionary<string, string> {
    // omitted
};

var notification = new TemplateNotification(templateProperties);

notification.Headers.Add("ServiceBusNotification-Apns-Expiry", DateTimeOffset.UtcNow.AddDays(7).ToString("yyyy-MM-ddTHH:mmzzz"));
notification.Headers.Add("apns-expiration", DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds().ToString());

await hubClient.SendNotificationAsync(notification, destinationTags);

并从 AppleTemplateRegistrationDescription 的初始化中删除了 Expiry 属性:

var apnsHeaders = new Dictionary<string, string> { { "apns-priority", "10" } };
registration = new AppleTemplateRegistrationDescription(deviceUpdate.Handle, PushNotifications.IosTemplate, apnsHeaders)
{
    Priority = "10"
};

查看 GitHub 上的 Microsoft.Azure.NotificationHubs 来源很有用。