5

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!

Vasiliy Kulakov
  • 5,401
  • 1
  • 20
  • 15
  • 2
    It might be a bug. I've seen this question asked before. – matt Jan 20 '17 at 18:05
  • Here's an idea. Try a much bigger interval, like 600. Does it fire immediately now? – matt Jan 20 '17 at 18:12
  • @matt it still fires with very large intervals – Vasiliy Kulakov Jan 20 '17 at 18:24
  • Another idea: Are you testing on a device? – matt Jan 20 '17 at 20:21
  • No, on an emulator. I will try testing on a device in a few weeks, but how do you think it might be related to the issue I'm seeing? – Vasiliy Kulakov Jan 24 '17 at 19:17
  • The emulator is only an emulator. There are _lots_ of differences between how it behaves and how the device behaves, especially in regard to things the runtime is supposed to do (or not do) in the background, so how do we know _a priori_ that this is not one of them? – matt Jan 24 '17 at 20:25
  • I am experiencing this as well, on a device. Feels like a bug to me. – Troy Feb 25 '17 at 23:24

1 Answers1

0

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

faircloud
  • 687
  • 6
  • 14