【问题标题】:Schedule Local Notification in iOS / irregular time interval在 iOS 中安排本地通知/不定期的时间间隔
【发布时间】:2016-10-25 09:44:13
【问题描述】:

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

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

请注意,我不能使用推送通知。

我怎样才能做到这一点?

【问题讨论】:

  • 您的应用仅支持 iOS 10 还是需要支持早期版本? iOS 10 的触发更加灵活。如果您需要支持早期版本,那么您需要安排多个通知,并在每次用户启动您的应用时重新安排新通知。

标签: ios push-notification notifications uilocalnotification nsnotificationcenter


【解决方案1】:

2 小时 = 60 秒 * 60 分钟 *2 = 7200 秒。这行得通吗?如果您在以下代码中的 TimeInterval 中使用 7200 为 5 秒 (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 10。 iOS 9 也有解决方案吗?
  • 请看 UILocalNotification 的例子。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 2015-03-28
    相关资源
    最近更新 更多