我的应用程序为将来安排了多个 UNNotificationRequest。每个请求都有一个唯一标识符。这不是在 UNCalanderNotificationTrigger 上重复的情况,因为重复设置为 NO。相反,每个请求都设置为不同的未来日历日期,但它们都是连续请求的,而且基本上是同时请求的。每个触发器由传入以下方法的间隔 (NSTimeInterval) 设置。UNNotificationCenter 委托 (appDelegate) 随后不会收到任何请求,无论是应用程序在后台还是前台。Apple 的开发人员文档都没有相关示例。我想知道我是否应该在完成处理程序中检查完成(我的代码有 withCompletionHandler: nil,但这就是 Apple 示例中显示的内容)。
-(UNNotificationRequest *)triggerNotifications: (NSString *)identifier : (NSTimeInterval) interval{
// Note: identifier must be unique or else each new request causes all others to be cancelled.
NSLog(@"interval: %f", interval);
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = NSLocalizedString(@"Timer expired", nil);
content.body = NSLocalizedString(@"Touch to continue", nil);
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = @"com.nelsoncapes.localNotification";
// NSDate *today = [NSDate date];
// NSDate *fireDate = [today dateByAddingTimeInterval:interval];
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:interval];
// first extract the various components of the date
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:fireDate];
NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:fireDate];
NSInteger second = [calendar component:NSCalendarUnitSecond fromDate:fireDate];
//then make new NSDateComponents to pass to the trigger
NSDateComponents *components = [[NSDateComponents alloc]init];
components.hour = hour;
components.minute = minute;
// components.second = second;
// construct a calendarnotification trigger and add it to the system
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: identifier content:content trigger:trigger];
NSLog(@"request: %@", request);
[center addNotificationRequest:request withCompletionHandler:nil];
return request;