我在这里所有问题中都搜索过,但是没有解决方案。
i使用UserInfo选项安排了LocalNetification。当应用在前景时,通知到达,并且可以使用系统单独调用的此功能完美处理。
-
对于本地通知
func application(_ application: UIApplication, didReceive notification: UILocalNotification) { if application.applicationState == .active { return } //My implementation }
-
用于远程通知
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { if application.applicationState == .active { return } //My implementation }
,但是问题是,当应用程序关闭时,此功能未调用,我无法处理所需的数据。
我尝试在AppDelegate中的该功能中获取UserInfo:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let localUserInfo = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? [AnyHashable: Any] {
// My implementation
} else if let remoteUserInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
// My implementation
}
return true
}
但是..没有工作...
有人可以帮我吗?
预先感谢。
您是否注册了通知?在didfinishlaunchingwithoptions((中:
// Register Notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
if granted {
print("User notifications are allowed")
} else {
print("User notifications are NOT allowed")
}
})
application.registerForRemoteNotification()
UNUserNotificationCenter.current().delegate = self
然后,您应该在UnusernotificationCenterdelegate方法中捕获本地通知:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
}
如果用户单击它是本地还是远程,您都会在didFinishLaunchingWithOptions
中获取通知