如何在UNNotificationRequest

时间:2017-11-06 11:03:33

标签: ios objective-c iphone xcode uilocalnotification

以前我在我的申请中使用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那样设置?

1 个答案:

答案 0 :(得分:2)

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

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

每周重复一遍:
同时传递所需的工作日组件:工作日小时分钟
⟶触发器将在当时的工作日每周触发

这是一个明天同时发出通知并每周重复通知的示例:

目标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)