【问题标题】:how to set alert time interval notification when close app swift快速关闭应用程序时如何设置警报时间间隔通知
【发布时间】:2018-11-22 10:36:52
【问题描述】:

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

【问题讨论】:

  • 你的问题很不清楚。你能准确描述一下吗?
  • 我想知道我们只关闭应用程序时设置警报通知的时间间隔

标签: swift notifications local


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多