以前,我在我的应用程序中使用UILocalNotification作为提醒用途。
但是如前所述,API在iOS中被否决了,现在,10.0需要使用UserNotifications框架的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框架的UNNotificationRequest实现这一点。
因为在新的api中,我们可以使用触发器UNCalendarNotificationTrigger和UNTimeintervalNotificationTrigger来启动UNNotificationRequest。
是否有任何可用的方法可以像UILocalNotification一样设置这两种方式?
发布于 2017-11-11 11:23:34
您仍然可以使用火日期和重复间隔,但它并不像以前那么明显。初始化通知触发器时,可以将其重复参数设置为YES。重复间隔由通过dateMatching参数传递给触发器的date组件定义:
每日重复:
只需传递日期组件hour,minute,second
⟶触发器在那个时候每天都会开火
每周重复:
还传递所需的工作日组件:工作日、hour、minute、second
⟶触发器将在那个工作日每周发射一次。
下面是一个例子,它在明天同一时间发出通知,并每周重复一次:
Objective-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];Swift:
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)https://stackoverflow.com/questions/47135461
复制相似问题