ios - 在iOS中安排本地通知/不定期的时间间隔

标签 ios push-notification notifications uilocalnotification nsnotificationcenter

我正在安排本地通知。我的问题是,周期性通知只能按每小时,每天,每周,每月或每年的时间间隔进行安排。您没有“每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/

相关文章:

ios - 单独文件中的类别给了我 "linker command failed with exit code 1 (use -v to see invocation)"

c++ - 在 XCode 4.6.2 上使用 cryptopp 静态库在 iOS 应用程序中抛出异常

ios - Swift App - 从 AnyObject 到 NSArray 的转换似乎失败

ios - 当应用程序已经在 iOS 上运行时,像本地通知一样推送通知?

java - 安排黑莓应用程序

ios - 在一个 tableviewcell 中所做的更改会影响其他单元格

安卓 : How to bypass Do Not Disturb So that it plays notification audio in background

amazon-web-services - 如何从 WCF 休息服务向 Kindle Fire 发送推送通知

android - 从状态栏中的扩展通知中获取文本?

Android: Intent 通过通知传递给 Activity ......我没有在 Intent 中获得正确的额外内容