【问题标题】:How to Set repeat interval as well as fire date in UNNotificationRequest如何在 UNNotificationRequest 中设置重复间隔和触发日期
【发布时间】:2017-11-06 11:03:33
【问题描述】:

以前我在我的应用程序中使用UILocalNotification 进行提醒。

但如上所述 API 在 iOS 10.0 中已弃用,现在需要使用 UserNotifications Framework 的UNNotificationRequest

使用UILocalNotification 我可以设置触发日期和重复间隔,如下所示:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];
    localNotification.fireDate = tomorrow;


    if(repeatUnit!=0){
          localNotification.repeatInterval = NSWeekCalendarUnit;
    }

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

在上面的代码中,我可以设置明天的通知将触发的日期,并且我设置了 1 周的重复间隔,这将从触发日期开始。

如何使用 UserNotifications Framework 的 UNNotificationRequest 来实现这一点。

因为在新的 api 中我们可以使用触发器UNCalendarNotificationTriggerUNTimeintervalNotificationTrigger 来启动UNNotificationRequest

有什么方法可以像UILocalNotification 那样设置吗?

【问题讨论】:

    标签: ios objective-c iphone xcode uilocalnotification


    【解决方案1】:

    您仍然可以使用触发日期和重复间隔,但它不像以前那么明显了。初始化通知触发器时,将其重复参数设置为YES。重复间隔由您通过dateMatching 参数传递给触发器的日期组件定义:

    每天重复:
    只需传递日期组件小时分钟
    ⟶ 触发器每天都会在那个时候触发

    每周重复:
    同时传递所需的工作日组件:weekdayhourminutesecond
    ⟶ 触发器将在每周的那个工作日触发

    这是一个示例,它在明天的同一时间触发通知并每周重复一次:

    目标-C:

    UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
    
    // find out what weekday is tomorrow
    NSDate *sameTimeTomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
    NSInteger weekdayTomorrow = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow];
    
    // set the current time (without weekday)
    unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute |  NSCalendarUnitSecond;
    NSDateComponents *firstFireDateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:sameTimeTomorrow];
    
    // add tomorrow's weekday
    firstFireDateComponents.weekday = weekdayTomorrow;
    
    // set a repeating trigger for the current time and tomorrow's weekday
    // (trigger repeats every week on that weekday at the current time)
    UNCalendarNotificationTrigger *notificationTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:firstFireDateComponents repeats:YES];
    
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification" content:notification trigger:notificationTrigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler: nil];
    

    斯威夫特:

    let notification = UNMutableNotificationContent()
    
    // find out what weekday is tomorrow
    let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24))
    
    // set the current time (without weekday)
    var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
    
    // add tomorrow's weekday
    firstFireDateComponents.weekday = weekDayTomorrow
    
    // set a repeating trigger for the current time and tomorrow's weekday
    // (trigger repeats every week on that weekday at the current time)
    let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true)
    
    let request = UNNotificationRequest(identifier: "notification", content: notification, trigger: notificationTrigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    

    【讨论】:

    • 不错。谢谢。
    • 我认为还应该注意的是,不可能同时设置fireDaterepeatInterval 以“从现在开始每分钟安排一次通知触发”。 ?
    猜你喜欢
    • 2019-11-13
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多