Repeating local notification is triggered immediately ― how to defer?

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

My goal is to set a notification that will occur N seconds in the future for the first time, and then repeat every N seconds.

However, creating a repeating notification seems to trigger the UNUserNotificationCenterDelegate immediately.

App delegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {     let center = UNUserNotificationCenter.current()     center.delegate = self     return true }  func startRequest() {     let content = UNMutableNotificationContent()     content.body = bodyText     content.categoryIdentifier = categoryIdentifier     content.sound = UNNotificationSound.default()      let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)     trigger.nextTriggerDate()     let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)      let center = UNUserNotificationCenter.current()     center.add(request) }  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {     // This callback received right after the request is made     completionHandler([.alert, .sound]) } 

I can work around this by creating a non-repeating notification and then starting repeating notifications when it expires. However, I was hoping there was some way to specify a first trigger date -- if memory serves right, the old notification API had this ability, and perhaps I am misunderstanding this new API

Thanks!

回答1:

The issue may be that you aren't setting the title on the UNMutableNotificationContent().

content.title = "Title goes Here" 

Also, to see if there is an error adding the request, you can add the following code.

center.add(request) { (error : Error?) in    if let theError = error {      // Handle any errors    } } 

I got this from Apple's docs - https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!