我看不到对安排此类通知的支持,即仅在特定时间过去时重复。问题是计划立即触发,而无需等待确切时间,尽管重复正确。
例如,这里是安排 10 分钟后重复通知的代码。这不会等待 10 分钟 - 从现在开始的下一分钟立即触发。
NSDate *theDate = [NSDate dateWithTimeIntervalSinceNow:60*10];
NSDateComponents *dateForSchedule = [[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] components:NSCalendarUnitMinute|NSCalendarUnitHour|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:theDate];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.body = "Your notification is here.";
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = CALL_NOTIFICATION_CATEGORY;
NSDateComponents* date = [[NSDateComponents alloc] init];
if ([self.selectedFrequecy isEqualToString:HOURLY]) {
date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:DAILY]) {
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:WEEKLY]) {
date.weekday = dateForSchedule.weekday;
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute
} else if ([self.selectedFrequecy isEqualToString:MONTHLY]) {
date.day = dateForSchedule.day;
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:YEARLY] || [self.selectedFrequecy isEqualToString:ONCE]) {
date.month = dateForSchedule.month;
date.day = dateForSchedule.day;
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute;
}
UNCalendarNotificationTrigger* trigger;
if ([_selectedFrequecy isEqualToString:ONCE]) {
trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
} else {
trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
}
// Create the request object.
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:self.callSchedule.id content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
{
if (error != nil) {
NSLog(@"%@", error.localizedDescription)
}
}];
这是另一个重复通知的代码块,它将在 3 天后每天触发。
NSDate *theDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSDateComponents *dateForSchedule = [[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] components:NSCalendarUnitMinute|NSCalendarUnitHour|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:theDate];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.body = "Your notification is here.";
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = CALL_NOTIFICATION_CATEGORY;
NSDateComponents* date = [[NSDateComponents alloc] init];
if ([self.selectedFrequecy isEqualToString:HOURLY]) {
date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:DAILY]) {
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:WEEKLY]) {
date.weekday = dateForSchedule.weekday;
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute
} else if ([self.selectedFrequecy isEqualToString:MONTHLY]) {
date.day = dateForSchedule.day;
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute;
} else if ([self.selectedFrequecy isEqualToString:YEARLY] || [self.selectedFrequecy isEqualToString:ONCE]) {
date.month = dateForSchedule.month;
date.day = dateForSchedule.day;
date.hour = dateForSchedule.hour;
date.minute = dateForSchedule.minute;
}
UNCalendarNotificationTrigger* trigger;
if ([_selectedFrequecy isEqualToString:ONCE]) {
trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
} else {
trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
}
// Create the request object.
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:self.callSchedule.id content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error)
{
if (error != nil) {
NSLog(@"%@", error.localizedDescription)
}
}];
这也不会等待 3 天,而是会在第二天立即触发。
两者的主要问题是它忽略了它应该等待开始触发通知的时间。如果有人了解问题所在,您是否会花一些时间来找出问题所在或是否可能。我会很感激的。