在 iOS / 不规则时间间隔内安排本地通知

Schedule Local Notification in iOS / irregular time interval

我正在安排本地通知。我的问题是重复通知只能按每小时、每天、每周、每月或每年的时间间隔安排。您没有“每 2 小时一次”的选项。

现在,我想每隔 2 或 3 小时安排一次。此外,由于有 iOS 的限制,一次只能安排 64 个通知。

请注意,我无法使用推送通知。

我怎样才能做到这一点?

2 小时 = 60 秒 * 60 分钟 *2 = 7200 秒。这行得通吗?如果您在以下代码中的 TimeInterval 中使用 7200,持续五秒 (from Apple website)

 let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default() // Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)

编辑:请查看使用 UILocalNotification 的类似代码

UILocalNotification* localNotification = [[UILocalNotificationalloc] init]; 
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7200];
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

如果您想每 2 小时安排一次通知,则必须安排 12 次通知,间隔 2 小时,并且每天重复一次。这将允许您每 2 小时安排一次通知以无限期重复,而不会超过您可以设置的通知数量。