可以有多个未完成的 UNNotificationRequest 吗?
Posted
技术标签:
【中文标题】可以有多个未完成的 UNNotificationRequest 吗?【英文标题】:Can there be more than one outstanding UNNotificationRequest? 【发布时间】:2017-11-07 00:17:09 【问题描述】:我的应用为未来时间安排了多个 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;
【问题讨论】:
【参考方案1】:答案是“是”,可以有多个未完成的 NSNotificationRequest。以下代码有效:
-(UNNotificationRequest *)triggerNotifications: (NSString *)identifier : (NSTimeInterval) interval
// Note: identifier must be unique or else each new request causes all others to be cancelled.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = NSLocalizedString(@"Timer expired", nil);
content.body = NSLocalizedString(@"Touch to continue", nil);
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
BOOL sound = [storage boolForKey:@"sound permission granted"];
if(sound)
if([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeyDoorBell, nil)])
content.sound = [UNNotificationSound soundNamed:@"doorbell.caf"];
else if ([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeySystemDefault, nil)])
content.sound = [UNNotificationSound defaultSound];
else if ([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeyElectronicChime, nil)])
content.sound = [UNNotificationSound soundNamed:@"electronic_chime.caf"];
else
if([self.selectedSound isEqual:NSLocalizedString(kselectedSoundKeyComputer, nil)])
content.sound = [UNNotificationSound soundNamed:@"Computer.caf"];
content.categoryIdentifier = @"com.nelsoncapes.localNotification";
NSDate *today = [NSDate date];
NSDate *fireDate = [today dateByAddingTimeInterval:interval];
// first extract the various components of the date
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger year = [calendar component:NSCalendarUnitYear fromDate:fireDate];
NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:fireDate];
NSInteger day = [calendar component:NSCalendarUnitDay fromDate:fireDate];
NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:fireDate];
NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:fireDate];
NSDateComponents *components = [[NSDateComponents alloc]init];
components.year = year;
components.month = month;
components.day = day;
components.hour = hour;
components.minute = minute;
// 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];
[center addNotificationRequest:request withCompletionHandler:^(NSError *error)
if(error)
NSLog(@"error on trigger notification %@", error);
];
return request;
【讨论】:
以上是关于可以有多个未完成的 UNNotificationRequest 吗?的主要内容,如果未能解决你的问题,请参考以下文章