swift - 快速关闭应用程序时如何设置警报时间间隔通知
问题描述
这是我的功能。我的功能在打开应用程序并在 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)")
}
}
}
解决方案
我找到了一种解决方案,它对我有用我在 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()
}
然后当我们关闭应用程序通知时,将警报跟随触发器的条件
推荐阅读
- java - 在另一个 JSP 页面上打印 JSP 表单数据
- python - 如何从 KIVY 桌面应用程序打印文件
- sql - NCLOB 到 VARCHAR2
- ruby-on-rails - collection_select: TypeError: ["foo","bar","baz"] 不是符号也不是字符串
- javascript - 如何在 JavaScript 中的正则表达式模式中提供空间
- android - 键盘上缺少 RecyclerView 项目出现
- selenium - 无法单击通过硒保存在列表中的第二个链接
- javascript - 节点中的原始文件名
- python - 不同的行值作为列在另一个字段上分组
- .net - F# 匹配有区别的联合