iOS 10通知触发并每分钟重复一次
我想在2分钟内触发通知,从现在开始,我还想每分钟重复一次。以下代码的问题是,它将每分钟重复一次,但会立即启动,而不是在2分钟内。谢谢你的帮助iOS 10通知触发并每分钟重复一次,ios,ios10,unusernotificationcenter,Ios,Ios10,Unusernotificationcenter,我想在2分钟内触发通知,从现在开始,我还想每分钟重复一次。以下代码的问题是,它将每分钟重复一次,但会立即启动,而不是在2分钟内。谢谢你的帮助 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.title = @"Good morning"; content.body = @"Body"; content.sound = [UNNotificationSound defaultSo
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);
}];
为什么不使用
UNTimeIntervalNotificationTrigger
而不是uncalendaNotificationTrigger
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 repeats:YES];
希望这个答案会有帮助。当您收到远程推送通知时,可以每2分钟重复一次 使用下面的方法来做这件事
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let action = response.actionIdentifier
let request = response.notification.request
let content = request.content
if action == "snooze.action"{
let snoozeTrigger = UNTimeIntervalNotificationTrigger(
timeInterval: 120.0,
repeats: true)
let snoozeRequest = UNNotificationRequest(
identifier: "snooze",
content: content,
trigger: snoozeTrigger)
center.add(snoozeRequest){
(error) in
if error != nil {
print("Snooze Request Error: \(String(describing: error?.localizedDescription))")
}
}
}
completionHandler()
}
这将在2分钟后开始,但不会在之后的每分钟重复。