在关闭应用程序时如何设置警报时间间隔通知

时间:2018-11-22 10:36:52

标签: swift notifications local

这是我的功能。我的功能是在打开应用程序并在10秒之前杀死应用程序时显示通知,但我想了解如何在仅关闭应用程序时捕获时间间隔

func showNotification(title: String, body: String) {
    // Configure the notification's payload.
    let content = contentNotification(title: title, body: body)
    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
    let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    // UNUserNotificationCenter.current().delegate = (self as! UNUserNotificationCenterDelegate)
    center.add(request) { (error: Error?) in
        if error != nil {
            // Handle any errors
            print("err Noti = \(error.debugDescription)")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了一种对我有用的解决方案 我在AppDelegate中调用我的函数在func applicationWillTerminate(_ application:UIApplication)中

func applicationWillTerminate(_ application: UIApplication) {

            // Called when the application is about to terminate. Save data if appropriate.   See also applicationDidEnterBackground:.
           // Saves changes in the application's managed object context before the application terminates. 

     NotificationManagerLocal().showNotification(title: "terminate Alert", body: "0000")

       CoreDataManager.sharedInstance.saveContext()
       //        self.saveContext()
  }

然后,当我们关闭应用程序通知时,将按照触发器的条件发出警报