应用程序关闭时如何处理localNotifications
How to handle localNotifications when app is closed
我在这里搜索了所有问题,但没有找到有效的解决方案。
我使用 userInfo 选项安排了 localNotifications。
当应用程序在前台时,通知到达并且可以完美处理这个系统自己调用的函数。
对于本地通知
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
中收到通知,否则您不会知道,除非将它们存储在服务器中并在每次打开应用程序时拉取它们
您是否注册了通知?
在 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()
}
}
我在这里搜索了所有问题,但没有找到有效的解决方案。
我使用 userInfo 选项安排了 localNotifications。 当应用程序在前台时,通知到达并且可以完美处理这个系统自己调用的函数。
对于本地通知
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
中收到通知,否则您不会知道,除非将它们存储在服务器中并在每次打开应用程序时拉取它们
您是否注册了通知? 在 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()
}
}