0

I have local notifications that fire up for specific date, for example, next monday. What i want is, to repeat this every monday. How to achieve that? Code i currently use:

 if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) {

        /* Trigger date */


        NSDate *date = [[NSDate date] mt_dateSecondsAfter:15];
        NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
                                         components:NSCalendarUnitYear +
                                         NSCalendarUnitMonth + NSCalendarUnitDay +
                                         NSCalendarUnitHour + NSCalendarUnitMinute +
                                         NSCalendarUnitSecond fromDate:date];

        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
                                                                                                          repeats:NO];

        /* Set notification */

        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.body = @"Время вставать с Ретро ФМ";
        content.categoryIdentifier = NotificationCategoryIdent;
        content.sound = [UNNotificationSound defaultSound];
        NSString *identifier = @"LocalNotification";
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                              content:content
                                                                              trigger:trigger];

        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Something went wrong: %@",error);
            }
        }];
    }
    else {

        // Code for old versions

        UILocalNotification *notification = [[UILocalNotification alloc] init];
        [notification setAlertBody:@"Время вставать с Ретро ФМ"];
        [notification setCategory:NotificationCategoryIdent];
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    }
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107

3 Answers3

1

Change your code:

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) {

        /* Trigger date */


        NSDate *date = [[NSDate date] mt_dateSecondsAfter:15];
        NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
                                         components:NSCalendarUnitYear +
                                         NSCalendarUnitMonth + NSCalendarUnitDay +
                                         NSCalendarUnitHour + NSCalendarUnitMinute +
                                         NSCalendarUnitSecond fromDate:date];

        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
                                                                                                          repeats:YES];

        /* Set notification */

        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.body = @"Время вставать с Ретро ФМ";
        content.categoryIdentifier = NotificationCategoryIdent;
        content.sound = [UNNotificationSound defaultSound];
        NSString *identifier = @"LocalNotification";
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                              content:content
                                                                              trigger:trigger];

        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Something went wrong: %@",error);
            }
        }];
    }
    else {

        // Code for old versions

        UILocalNotification *notification = [[UILocalNotification alloc] init];
        [notification setAlertBody:@"Время вставать с Ретро ФМ"];
        [notification setCategory:NotificationCategoryIdent];
        [notification setRepeatCalendar:NSCalendarUnitWeekday];
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    }
Sargis
  • 1,196
  • 1
  • 18
  • 31
1

You need to replace this in your code

UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
                                                                                                          repeats:YES];
KKRocks
  • 8,222
  • 1
  • 18
  • 84
1

The answer given , will work fine for iOS greater than Or equal. But for lower version (as question has if else clause) it will not work. For Bellow iOS it will

localNotification.repeatInterval = NSCalendarUnitDay

This set notification for every day

Abhik Das
  • 11
  • 2