我正在安排本地通知。我的问题是,周期性通知只能按每小时,每天,每周,每月或每年的时间间隔进行安排。您没有“每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];
关于ios - 在iOS中安排本地通知/不定期的时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40236606/