【问题标题】:Repeat local notification for specific date iOS 10针对特定日期重复本地通知 iOS 10
【发布时间】:2017-05-11 11:54:40
【问题描述】:

我有在特定日期触发的本地通知,例如下周一。我想要的是,每周一重复这个。如何做到这一点?我目前使用的代码:

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

【问题讨论】:

  • 这段代码有什么问题?
  • 只是重复:NO 到重复:YES
  • @Koen 它的工作,我希望每个星期一/月/日重复一次
  • 您必须从日期组件每周设置一次
  • repeatInterval,NSCalendarUnitWeekday 会帮助你

标签: ios objective-c


【解决方案1】:

你需要在你的代码中替换它

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

【讨论】:

  • 它的重复日期和触发日期究竟是什么?开火日期之后的下一个日期?
【解决方案2】:

更改您的代码:

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

【讨论】:

  • 有什么区别?
  • UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:YES];
  • 和[通知集RepeatCalendar:NSCalendarUnitWeekday];
【解决方案3】:

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

localNotification.repeatInterval = NSCalendarUnitDay

每天设置通知

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多