首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以有多个未完成的UNNotificationRequest?

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

Stack Overflow用户
提问于 2017-11-07 08:17:09
回答 1查看 222关注 0票数 0

我的应用程序为将来的时间安排了多个UNNotificationRequest。每个请求都有一个唯一的标识符。这不是在UNCalanderNotificationTrigger上使用repeat的情况,因为repeat设置为NO。相反,每个请求被设置为不同的未来日历日期,但它们都是连续且基本上在同一时间请求的。每个触发器由传递给以下方法的间隔(NSTimeInterval)设置。随后,无论是应用程序在后台还是在前台,UNNotificationCenter委托(appDelegate)都不会收到任何请求。苹果的开发者文档中没有一个相关的例子。我想知道我是否应该在完成处理程序中检查完成(我的代码有withCompletionHandler: nil,但这是苹果的示例中显示的)。

代码语言:javascript
复制
    -(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;
EN

回答 1

Stack Overflow用户

发布于 2017-12-01 09:17:39

答案是肯定的,可以有多个未完成的NSNotificationRequest。下面的代码可以工作:

代码语言:javascript
复制
   -(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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47147991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档