繁体   English   中英

是否可以有多个未完成的UNNotificationRequest?

[英]Can there be more than one outstanding UNNotificationRequest?

我的应用为将来的时间安排了多个UNNotificationRequest。 每个请求都有一个唯一的标识符。 这不是在UNCalanderNotificationTrigger上重复的情况,因为重复设置为NO。 而是,每个请求都设置为一个不同的未来日历日期,但是它们都是在实质上同时串行发送的。 每个触发器由传递给以下方法的间隔(NSTimeInterval)设置。 当应用程序在后台或前台时,UNNotificationCenter委托(appDelegate)随后都不会收到任何请求。 Apple的Developer文档中都没有相关示例。 我想知道是否应该在完成处理程序中检查是否完成(我的代码具有withCompletionHandler:nil,但这就是苹果示例中显示的内容)。

    -(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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM