针对特定日期iOS 10重复本地通知

时间:2017-05-11 11:54:41

标签: ios objective-c

我有针对特定日期的本地通知,例如,下周一。我想要的是,每个星期一重复这个。怎么实现呢?我目前使用的代码:

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

3 个答案:

答案 0 :(得分:1)

更改您的代码:

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

答案 1 :(得分:1)

您需要在代码中替换它

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

答案 2 :(得分:1)

给出的答案,适用于大于或等于iOS的iOS。但对于较低版本(因为问题有if else子句)它将无法正常工作。对于Bellow iOS,它将

localNotification.repeatInterval = NSCalendarUnitDay

每天设置通知