0

私のアプリは、将来のために複数の UNNotificationRequest をスケジュールします。各リクエストには一意の識別子があります。これは、繰り返しが NO に設定されているため、UNCalanderNotificationTrigger で繰り返しを行う場合には当てはまりません。むしろ、各リクエストは異なる将来のカレンダー日付に設定されますが、それらはすべて連続して、本質的に同時にリクエストされます。各トリガーは、次のメソッドに渡される間隔 (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;
4

1 に答える 1