Nexttriggerdate() Doesn't Return the 'Expected' Value, Is There Another Way to Obtain the Next Fire Date of a Repeating Local Notification

nextTriggerDate() doesn't return the 'expected' value, is there another way to obtain the next fire date of a repeating Local Notification?

Confirmed. I ran this code:

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)
print("scheduling at", Date())
DispatchQueue.main.asyncAfter(deadline: .now()+15) {
print("checking at", Date())
UNUserNotificationCenter.current().getPendingNotificationRequests {
arr in let arr = arr
if let req = arr[0].trigger as? UNTimeIntervalNotificationTrigger {
let fd = req.nextTriggerDate()
print("trigger date", fd as Any)
}
}
}
// ... proceed to configure and schedule the notification

I also configured my user notification center delegate to receive the notification in the foreground and print the time.

Here's what I saw in the console:

scheduling at 2018-08-01 03:40:36 +0000
checking at 2018-08-01 03:40:51 +0000
trigger date Optional(2018-08-01 03:42:51 +0000)
received notification while active 2018-08-01 03:42:36 +0000

So the trigger date was reported as 2 minutes from when I checked, but the notification actually fired 2 minutes from when I scheduled it.

I'd describe that as a bug!

The one disagreement I'd have with your original question is that I get exactly the same result for a non-repeating UNTimeIntervalNotificationTrigger:

scheduling at 2018-08-01 03:45:50 +0000
checking at 2018-08-01 03:46:06 +0000
trigger date Optional(2018-08-01 03:48:06 +0000)
received notification while active 2018-08-01 03:47:50 +0000

A UNTimeIntervalNotificationTrigger also has a timeInterval property, but even that does us no good unless we know when the notification was originally scheduled — and a UNNotificationRequest provides no way to find that out.

(Just to be certain, I postponed my checking until the repeating notification had fired a couple of times, but the result was the same: nextTriggerDate is clearly adding the timeInterval to now rather than reporting the time at which the notification will next fire.)

Repeat notification that shows current date

alertBody is just a NSString property. So it is not computed each time notification is shown. That is why you can not show "changeable" text there.

If for some reason you need to show current "date" in the notification, try to change the displayed text to "now" or display only scheduled time there without "day" component.

changing repeats value of UNTimeIntervalNotificationTrigger

You can remove the local notification as

UIApplication.shared.cancelAllLocalNotifications()

And if you want to true again then again assign

trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)

May be this Work

Creating a local notification to fire at a specific time

initially check your date format it is in wrong dateFormatter.dateFormat = "YYY-MM-dd", it is in dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

for e.g

let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message "
localNotification1.timeZone = NSCalendar.currentCalendar().timeZone
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
localNotification1.fireDate = formatter.dateFromString("2016-08-30 22:38:00")

UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)

cancelAllLocalNotifications With Multiple LocalNotifications

Yes, as you see. cancelAllLocalNotifications, as it's name suggests, will cancel everything. Yes, also, you should use the userInfo to differentiate between your notifications.

To manage / remove your notifications selectively you should get all of the registered notifications with scheduledLocalNotifications, then iterate over them checking the userInfo, then call cancelLocalNotification with the ones you no longer need.



Related Topics



Leave a reply



Submit