【发布时间】:2018-11-24 16:44:18
【问题描述】:
这是我的代码的一部分。我想使用UNUserNotificationCenter 和UNUserNotificationCenterDelegate 来处理通知事件。
此代码在应用处于前台状态时捕获通知事件。但是"didReceive" 不会因为后台状态而被触发。
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("willPresent") /// This works
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("didReceive") /// This doesn't work
completionHandler()
}
但如果我不使用UNUserNotificationCenter.current().delegate = self,委托方法会在后台正确触发。
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping FetchCompletionHandler) {
print("didReceiveRemoteNotification... \(userInfo)")
}
如何使用“didReceive”?我想在后台处理通知。
【问题讨论】:
标签: ios swift push-notification unusernotificationcenter