0

My app schedules multiple UNNotificationRequest for future times. Each request has a unique identifier. This is not the case of having a repeat on a UNCalanderNotificationTrigger, since repeat is set to NO. Rather, each request is set for a different future calendar date, but they are all requested serially and at essentially the same time. Each trigger is set by the interval (NSTimeInterval) that is passed in to the following method. None of the requests are subsequently received in the UNNotificationCenter delegate (appDelegate), either when the app is in the background or in the foreground. None of Apple's Developer documentation has a relevant example. I am wondering if I should be checking for completion in a completion handler (my code has withCompletionHandler: nil, but that is what is shown in Apple's examples).

    -(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;
Nelson Capes
  • 411
  • 3
  • 12

1 Answers1

0

The answer is "yes", there can be multiple outstanding NSNotificationRequest. The following code works:

   -(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;
}
Nelson Capes
  • 411
  • 3
  • 12