应用终止后未调用本地通知委托方法

时间:2017-07-07 11:10:08

标签: swift localnotification usernotifications

我可以在终止后生成本地通知,但是我无法在UNUserNotificationCenterDelegate的didReceive方法中设置值。

以下是我在AppDelegate中的代码:

class AppDelegate: UIResponder, UIApplicationDelegate{

    var window: UIWindow?
    let preferences = UserDefaults.standard
    let keyForTappingNotification = "notification_tapped"

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //Requesting Authorization for User Interactions
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in

        }

        return true
    }

}

以下是我在ViewController中的代码:

func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping(UNNotificationPresentationOptions) -> Void) {
    completionHandler( [.alert,.sound,.badge])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
    preferences.set("yes", forKey: self.keyForTappingNotification)
    preferences.synchronize()

    completionHandler()
}

ViewdidLoad如下:

override func viewDidLoad() {

        super.viewDidLoad()

        if let notification_tapped = self.preferences.string(forKey: self.keyForTappingNotification) {

            if notification_tapped.lowercased() == "yes"{
                //call to specific function which never gets called as values don't match
            }

        }
}

委托方法仅在app处于前台或后台时才会收到响应,但在终止后才会收到响应。

有没有办法在应用终止后收到回复?

0 个答案:

没有答案