我希望在2分钟后触发通知,并且还想在每分钟重复一次 . 以下代码的问题是它会在每分钟重复,但它会在2分钟内立即开始 . 感谢任何帮助 .
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Good morning";
content.body = @"Body";
content.sound = [UNNotificationSound defaultSound];
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:120];
NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
components:NSCalendarUnitSecond fromDate:date];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:YES];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification.daily" content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@", error);
}];
2 回答
为什么不使用
UNTimeIntervalNotificationTrigger
而不是UNCalendarNotificationTrigger
.希望这个答案会有所帮助 .
收到远程推送通知后,您可以在每2分钟后重复一次
使用以下方法来执行此操作 .