Я столкнулся с странной проблемой: UNTimeIntervalNotificationTrigger triggerWithTimeInterval:repeats:
запускается с константой, такой как 10
, но не запускается с NSTimeInterval variable
.
ios 12.1.2.
в следующем коде, если я использую вычисленную interval
в
UNTimeIntervalNotificationTrigger *trigger =
[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:interval repeats:NO];
уведомление не сработало.
если я использую константу, выдается уведомление:
UNTimeIntervalNotificationTrigger *trigger =
[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
вот фрагмент кода.
- (void)addLocalNotificationForNewVersionWithSubject:(NSString *)subject
body:(NSString *)body
sendAtTime:(NSDate *)sendAtTime API_AVAILABLE(ios(10)) {
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = subject;
content.body = body;
content.sound = [UNNotificationSound defaultSound];
// timeIntervalSinceDate returns negative value for future date, positive value for past date. but UNTimeIntervalNotificationTrigger needs positive value for future date. this is what (0 - timeIntervalSinceDate) means.
// UNTimeIntervalNotificationTrigger throws exception if interval parameter is not greater than 0. so for negative value for past date, i send the notificaion 10 seconds in the future.
NSTimeInterval interval = (0 - [[NSDate date] timeIntervalSinceDate:sendAtTime]);
if (interval <= 0) {
interval = 10;
}
// here is the difference.
UNTimeIntervalNotificationTrigger *trigger =
[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:interval repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest
requestWithIdentifier:@“some_identifier" content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
if (error) {
NSLog(@“add notification failure:%@", error);
return;
}
NSLog(@"add notification successful, %@", request);
}];
}
кто-нибудь знает что-нибудь об этом?
спасибо!