在 Swift 2 中的特定时间安排本地通知
Schedule a local notification for a specific time in Swift 2
我遍历了这些论坛和其他网站,但不断收到一些不相符的答案。本质上,我想创建一个通知,例如,每个工作日 6:28 AM、12:28 PM 和 5:28 PM。
我有一些解决方案,但我真的不确定去哪里。我设置正确吗?感谢任何帮助。
let notification: UILocalNotification = UILocalNotification()
notification.category = "News and Sports"
notification.alertAction = "get caught up with the world"
notification.alertBody = "LIVE news and sports on VIC in just a minute!"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
准备显示本地通知需要 2 个主要步骤:
步骤 1
On iOS 8+ 您的应用程序必须请求并随后获得用户的许可才能显示本地通知。可以在您的 AppDelegate 中按如下方式请求许可。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if #available(iOS 8, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
}
return true
}
当您的应用在 iOS 8 之前的操作系统上 运行 时,请勿调用 registerUserNotificationSettings(_:)
。否则,您的应用程序将在运行时崩溃。 幸运的是,这应该不是问题,因为您正在使用 Swift 2.
第 2 步
安排在将来 fireDate
时通知您。
let notification:UILocalNotification = UILocalNotification()
...
... // set the notification's category, alertAction, alertBody, etc.
...
notification.fireDate = ... // set to a future date
UIApplication.sharedApplication().scheduleLocalNotification(notification)
不幸的是,根据@progrmr this Stack Overflow answer,
You cannot set custom repeat intervals with UILocalNotification. This has been asked before (see below) but
only limited options are provided. The repeatInterval parameter
is an enum type and it limited to specific values.
You cannot multiply those enumerations and get multiples of those
intervals. You cannot have more than 64 local notifications set in
your app. You cannot reschedule a notification once it fires unless
the user chooses to run your app when the notification fires (they may
not run it).
There is a request for repeat interval multipliers posted here.
You can add comments to it. I suggest filing a bug report or feature
request (url?) with Apple.
许多其他 Stack Overflow 答案证实了上述引述中的说法。访问 link 以获取完整引用的答案,其中包含支持答案的列表。
您的案例的一个潜在解决方法是安排 3 个本地通知。分别将每个设置为在 6:28 AM、12:28 PM 和 5:28 PM 触发。然后,将所有3个本地通知的repeatInterval
设置为.CalendarUnitWeekday
。
我遍历了这些论坛和其他网站,但不断收到一些不相符的答案。本质上,我想创建一个通知,例如,每个工作日 6:28 AM、12:28 PM 和 5:28 PM。
我有一些解决方案,但我真的不确定去哪里。我设置正确吗?感谢任何帮助。
let notification: UILocalNotification = UILocalNotification()
notification.category = "News and Sports"
notification.alertAction = "get caught up with the world"
notification.alertBody = "LIVE news and sports on VIC in just a minute!"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
准备显示本地通知需要 2 个主要步骤:
步骤 1
On iOS 8+ 您的应用程序必须请求并随后获得用户的许可才能显示本地通知。可以在您的 AppDelegate 中按如下方式请求许可。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if #available(iOS 8, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
}
return true
}
当您的应用在 iOS 8 之前的操作系统上 运行 时,请勿调用 registerUserNotificationSettings(_:)
。否则,您的应用程序将在运行时崩溃。 幸运的是,这应该不是问题,因为您正在使用 Swift 2.
第 2 步
安排在将来 fireDate
时通知您。
let notification:UILocalNotification = UILocalNotification()
...
... // set the notification's category, alertAction, alertBody, etc.
...
notification.fireDate = ... // set to a future date
UIApplication.sharedApplication().scheduleLocalNotification(notification)
不幸的是,根据@progrmr this Stack Overflow answer,
You cannot set custom repeat intervals with UILocalNotification. This has been asked before (see below) but only limited options are provided. The repeatInterval parameter is an enum type and it limited to specific values.
You cannot multiply those enumerations and get multiples of those intervals. You cannot have more than 64 local notifications set in your app. You cannot reschedule a notification once it fires unless the user chooses to run your app when the notification fires (they may not run it).
There is a request for repeat interval multipliers posted here. You can add comments to it. I suggest filing a bug report or feature request (url?) with Apple.
许多其他 Stack Overflow 答案证实了上述引述中的说法。访问 link 以获取完整引用的答案,其中包含支持答案的列表。
您的案例的一个潜在解决方法是安排 3 个本地通知。分别将每个设置为在 6:28 AM、12:28 PM 和 5:28 PM 触发。然后,将所有3个本地通知的repeatInterval
设置为.CalendarUnitWeekday
。