【问题标题】:Urban Airship receivedBackgroundNotification never calledUrban Airship 收到BackgroundNotification 从未调用
【发布时间】:2018-11-14 04:57:49
【问题描述】:
在阅读了所有指南并检查了互联网上的数百篇文章之后,我很确定从未调用过 receivedBackgroundNotification 方法。
一切都很完美,但是当应用程序在后台时,会显示一条通知并且永远不会调用此方法。似乎不可能让它工作。
假设所有的正常操作和基本配置都已经做好并且工作正常,我可以用这个库做些什么来拦截和管理后台推送通知?
如果有任何帮助,我将不胜感激。
【问题讨论】:
标签:
iphone
push-notification
urbanairship.com
【解决方案1】:
确保您已配置以下内容:
- 在目标功能中启用了远程通知后台模式
- 已在您的测试设备上启用后台应用刷新
- 假设您正在尝试使用 UAPushNotificationDelegate,请确保您拥有 automatic setup enabled 或将所有正确的方法转发到 UA SDK。
只有当您在负载中发送带有 content-available=1 的推送通知时,Apple 才会唤醒您的应用程序。该选项在composer中公开为“后台处理”,或者您可以在使用push api时将其设置在iOS overrides中。
【解决方案2】:
在 Urban Airship iOS SDK v.13.4.0 中,如果未调用 func receivedBackgroundNotification(_ notificationContent: UANotificationContent, completionHandler: @escaping (UIBackgroundFetchResult) -> Void),如果您将通知显示为警报,则可以在 func receivedNotificationResponse(_ notificationResponse: UANotificationResponse, completionHandler: @escaping () -> Void) 中处理通知。请记住,您应该只处理带有actionIdentifier == UNNotificationDefaultActionIdentifier 的通知(用户从通知界面打开应用程序)。
这是UAPushNotificationDelegate 实现的示例:
extension MyPushNotificationDelegate: UAPushNotificationDelegate {
public func receivedForegroundNotification(_ notificationContent: UANotificationContent, completionHandler: @escaping () -> Void) {
completionHandler()
}
public func receivedBackgroundNotification(_ notificationContent: UANotificationContent, completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(.newData)
}
public func receivedNotificationResponse(_ notificationResponse: UANotificationResponse, completionHandler: @escaping () -> Void) {
guard notificationResponse.actionIdentifier == UNNotificationDefaultActionIdentifier else {
completionHandler()
return
}
someFunc() {
completionHandler()
}
}
public func extend(_ options: UNNotificationPresentationOptions = [], notification: UNNotification) -> UNNotificationPresentationOptions {
[.alert, .badge, .sound]
}
}