10

Till iOS 9 we write local notifications like this

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];

and in local notification we have repeatInterval, Now in WWDC2016 Apple announced User Notification which contains

  • UNTimeIntervalNotificationTrigger.
  • UNCalendarNotificationTrigger.

    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
    

the code above will trigger notification after every minute. But can't set the date.

NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30;

UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];

the code above date can be set and repeat will trigger tomorrow at 8:30 not not after minute.

In iOS 10 User Notification how I can set date time with repeat frequency just like we can set in UILocalNotification?

I want to schedule User Notification tomorrow at 8:30pm and keep repeating after every minute just like the code I specified at the top regarding local notification

Nirav D
  • 71,513
  • 12
  • 161
  • 183
S.J
  • 3,063
  • 3
  • 33
  • 66
  • Hi all, I have the same question. Please let us know if anyone has implemented this functionality yet? **FYI:** The question is "How to set repeat intervals like everyday, every month, every minute etc for the local notifications in **iOS 10**". I am waiting. Thanks. – Muhammad Ibrahim Aug 30 '16 at 04:55
  • 1
    Found any solution for above problem? – Hiren Gujarati Nov 06 '16 at 12:14
  • Also very related: [UserNotification in 3 days then repeat every day/hour - iOS 10](http://stackoverflow.com/q/38380783/2415822) – JAL Nov 28 '16 at 21:31

2 Answers2

1

inspired by @Ramon Vasconcelos, I'm using the following code for setting up intervals and fireDate together

switch (interval) {
    case NSCalendarUnitMinute: {
        unitFlags = NSCalendarUnitSecond;
        break;
    }
    case NSCalendarUnitHour: {
        unitFlags = NSCalendarUnitMinute | NSCalendarUnitSecond;
        break;
    }
    case NSCalendarUnitDay: {
        unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        break;
    }
    case NSCalendarUnitWeekOfYear: {
        unitFlags = NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        break;
    }
    case NSCalendarUnitMonth:{
        unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    }
    case NSCalendarUnitYear:{
        unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    }
    default:
        unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        break;
}
NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:fireDate];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:interval != 0];
Kitson88
  • 2,889
  • 5
  • 22
  • 37
Libin Lu
  • 67
  • 6
0

For iOS 10 you can use like this:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate];
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

it has the same effect from the iOS 9 code. To repeat you just have to use the components that you want to repeat.

Ramon Vasconcelos
  • 1,466
  • 1
  • 21
  • 28