In UILocalNotification we use NSCalendarUnitMinute
like repetition ..... but I can't find in iOS 10 UserNotification doc ... How can I use NSCalendarUnitMinute
like repetition in iOS 10 UserNotification
?
here is the code which will schedule local notification at 8:30 pm and will repeat after every one minute.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.textField.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Use a
UNCalendarNotificationTrigger
withDateComponents
instead ofNSCalendar
:You set up the date with a
DateComponents
instance and specify if the notification should repeat with therepeats
parameter of theUNCalendarNotificationTrigger
initializer.UNCalendarNotificationTrigger Documentation
Just a heads up that there is a typo in the Apple Docs (as of 6/13). If you use
NSDateComponents
instead ofDateComponents
, you will need to explicitly cast yourdate as DateComponents
in thedateMatching
parameter.In response to your comment, I don't believe the behavior you want (changing the frequency of your repeated notification) is supported by
UNCalendarNotificationTrigger
.Although it appears that the old notifications framework had better support regarding this, it's only until, we realise that the new one is better!!
I had similar problems, and below is a sample of how the old notifications can go hand in hand with the new ones.
To be on a safer side, this is
Swift2.3
.Although this isn't exhaustive, I've pretty much covered everything from a minute to a year.
And to be more detailed,
As this works for me, it should probably for the rest of you. Please try and let me know if there is an issue.
And, for the exact solution to this question, I'd advise the below.
(Probably, Apple didn't think about such a scenario, but it can be handled)
NSCalendarUnitMinute
equivalent shown above.Bear with me this is my first post.
We needed the 1 minute repeat interval for our app in ios 10, and used as a workaround a combination the old and new framework.
scheduling the repeating local notifications with the old:
UILocalNotification *ln = [[UILocalNotification alloc] init];
...
ln.repeatInterval = kCFCalendarUnitMinute;
ln.userInfo = @{@"alarmID": ...}
...
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
This will create and schedule the usual repeating LNs that will show up as UNNotification with a UNLegacyNotificationTrigger
I used a alarmID to connect old and new framework.
Try:
With the new framework UNUserNotificationCenter methods: willPresentNotification: didReceiveNotificationResponse:
Hope this helps