【问题标题】:How to handle localNotifications when app is closed应用程序关闭时如何处理localNotifications
【发布时间】:2018-03-27 20:10:25
【问题描述】:

我在这里搜索了所有问题,但没有解决方案。

我使用 userInfo 选项安排了 localNotifications。 当app在前台时,通知到了,a可以很好的处理这个系统自己调用的函数。

  • 本地通知

    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
}

但是..没用...

有人可以帮帮我吗?

提前致谢。

【问题讨论】:

    标签: ios swift4 uilocalnotification userinfo remote-notifications


    【解决方案1】:

    您是否注册了通知? 在 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()
        }
    
    }
    

    【讨论】:

    • 我没有使用 UNNotificationCenter。为了获得许可,我正在使用 application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
    • registerUserNotificationSettings(UIUserNotificationSettings 自 iOS10 以来已弃用。改用用户 UNUserNotificationCenter。试试我的代码
    • 问题是我使用 UILocalNotifications 配置通知(我需要支持 ios 9.0 及更高版本)。而 UNUserNotificationCenter 仅适用于 ios 10.0 及更高版本....
    • 我使用了 UNUserNotificationCenter.current().delegate = self 并且工作了,谢谢!
    【解决方案2】:

    如果用户单击它是本地还是远程,您将在didFinishLaunchingWithOptions 中收到通知,否则您将不知道除非将它们存储在服务器中并在每次打开应用程序时拉取它们

    【讨论】:

    • 但是,就像我在问题中所说的那样,即使我在应用关闭时单击通知,launchOptions 也是 nil。
    • 你可以打印内容而不像 print(launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification])
    猜你喜欢
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多