以前我在我的申请中使用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中我们可以使用触发器UNCalendarNotificationTrigger
和UNTimeintervalNotificationTrigger
来启动UNNotificationRequest
。
是否有任何可用的设置,就像UILocalNotification
那样设置?
答案 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)