내 앱이 이후 UNNotificationRequest 일정을 예약합니다. 각 요청에는 고유 식별자가 있습니다. repeat가 NO로 설정되어 있기 때문에 UNCalanderNotificationTrigger에서 반복되는 경우가 아닙니다. 오히려 각 요청은 다른 미래 달력 날짜로 설정되지만 모두 연속적으로 그리고 본질적으로 동시에 요청됩니다. 각 트리거는 다음 메소드로 전달되는 간격 (NSTimeInterval)에 의해 설정됩니다. 응용 프로그램이 백그라운드 나 포 그라운드에있을 때 UNNotificationCenter 대리인 (appDelegate)에서 요청을받지 못합니다. Apple의 개발자 설명서에는 관련 사례가 없습니다. 완성 처리기에서 완성을 검사해야하는지 궁금합니다. (코드에는 withCompletionHandler : nil이 있지만, Apple의 예제에 나와 있습니다.)두 가지 이상의 UNNotificationRequest가있을 수 있습니까?
0
A
답변
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;
}