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

标签 ios objective-c nsdate uilocalnotification nsdatecomponents

<分区>

直到iOS 9我们这样写本地通知

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 分安排用户通知,并在每分钟后继续重复,就像我在顶部指定的关于本地通知

的代码一样

最佳答案

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

关于ios - 如何在用户通知中设置重复频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983437/

相关文章:

iphone - “保持” UIButton 行为 - `Touch Cancelled` 控制状态阻止进一步的控制状态

数组中多个日期的swift 2本地警报

iphone - 将秒转换为格式化时间,例如 12 :59?

ios - UIScrollView 拖动受 UIWindow 变换旋转影响

ios - swift 3 : How to access the value of matrix_float3x3 in a 48-byte CFData?

ios - 带有平移手势识别器的 CollectionViewCell

ios - Objective-C非弧形物体

iphone - 使用 UIBezierPath 绘制图形曲线

swift - 使用 Swift 查找 NSDates 之间的秒数差异作为整数

iphone - 在 iPad 应用程序中添加图像效果