2017-11-07 10 views
0

私のアプリは将来のために複数のUNNotificationRequestをスケジュールします。各要求には一意の識別子があります。 repeatはNOに設定されているため、UNCalanderNotificationTriggerでリピートすることはできません。むしろ、各要求は異なる将来のカレンダー日付に設定されますが、それらはすべてシリアルで、本質的に同時に要求されます。各トリガーは、次のメソッドに渡される間隔(NSTimeInterval)によって設定されます。アプリケーションがバックグラウンドまたはフォアグラウンドのいずれかにある場合は、後でUNNotificationCenterデリゲート(appDelegate)でリクエストが受信されません。 Appleの開発者向けドキュメントには関連する例はありません。完了ハンドラで完了をチェックする必要があるのか​​どうか疑問に思っています(私のコードにはwithCompletionHandler:nilがありますが、これはAppleの例に示されています)。未解決のUNNotificationRequestが複数存在することはありますか?

答えて

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; 
} 
関連する問題