如何在用户通知中设置重复频率

时间:2016-06-23 06:01:18

标签: ios objective-c nsdate uilocalnotification nsdatecomponents

直到iOS 9我们这样写local notifications

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

在本地通知中我们有repeatInterval,现在在WWDC2016 Apple宣布User Notification包含

  • UNTimeIntervalNotificationTrigger。
  • UNCalendarNotificationTrigger。

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

上面的代码会在每分钟后触发通知。但无法设定日期。

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

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

上面的代码可以设置,重复将在明天8:30触发,而不是在分钟后。

在iOS 10 User Notification中我如何设置重复频率的日期时间,就像我们可以在UILocalNotification中设置一样?

我希望明天晚上8:30安排User Notification并在每分钟之后继续重复,就像我在顶部指定的有关local notification

的代码一样

2 个答案:

答案 0 :(得分:1)

对于iOS 10,您可以这样使用:

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

它与iOS 9代码具有相同的效果。要重复,您只需使用要重复的组件。

答案 1 :(得分:1)

受到@Ramon Vasconcelos的启发,我正在使用以下代码来设置间隔和fireDate

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