可以有多个未完成的UNNotificationRequest吗?

时间:2017-11-07 00:17:09

标签: ios multiple-instances unnotificationrequest

我的应用程序计划多次UNNotificationRequest以供将来使用。每个请求都有唯一的标识符。这不是在UNCalanderNotificationTrigger上重复的情况,因为repeat设置为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 个答案:

答案 0 :(得分:0)

答案是“是”,可以有多个未完成的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;
}