首页 > 解决方案 > 快速关闭应用程序时如何设置警报时间间隔通知

问题描述

这是我的功能。我的功能在打开应用程序并在 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)")
        }
    }
}

标签: swiftnotificationslocal

解决方案


我找到了一种解决方案,它对我有用我在 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()
  }

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


推荐阅读