xcode 在 Swift 2 中为特定时间安排本地通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31043458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:22:10  来源:igfitidea点击:

Schedule a local notification for a specific time in Swift 2

xcodenotificationsuilocalnotificationswift2

提问by alexbredikin

I've been all over these forums and other sites and I keep getting pieces of an answer that don't add up. Essentially, I would like to create a notification that fires, for example, every weekday at 6:28 AM, 12:28 PM, and 5:28 PM.

我已经走遍了这些论坛和其他网站,我不断得到一些不合时宜的答案。本质上,我想创建一个通知,例如,在每个工作日的上午 6:28、下午 12:28 和下午 5:28 触发。

I have pieces of a solution, but I'm really unsure where to go. Am I setting this up right at all? Any help is appreciated.

我有一些解决方案,但我真的不确定该去哪里。我设置得对吗?任何帮助表示赞赏。

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)

回答by ndmeiri

Preparing to show local notifications requires 2 main steps:

准备显示本地通知需要两个主要步骤:

Step 1

第1步

On iOS 8+ your app must ask and, subsequently, be granted permission by the user to display local notifications. Asking permission can be done as follows in your AppDelegate.

在 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
}

Do not call registerUserNotificationSettings(_:)when your app is running on a pre-iOS 8 operating system. Otherwise, your app will crash at runtime.Luckily, this shouldn't be a problem since you're working with Swift 2.

registerUserNotificationSettings(_:)当您的应用在 iOS 8 之前的操作系统上运行时不要调用。否则,您的应用程序将在运行时崩溃。幸运的是,这应该不是问题,因为您正在使用 Swift 2。

Step 2

第2步

Schedule your notification at a future fireDate.

在将来安排您的通知fireDate

let notification:UILocalNotification = UILocalNotification()
...
... // set the notification's category, alertAction, alertBody, etc.
...
notification.fireDate = ... // set to a future date
UIApplication.sharedApplication().scheduleLocalNotification(notification)

Unfortunately, according to this Stack Overflow answerby @progrmr,

不幸的是,根据@progrmr 的Stack Overflow 回答

You cannot set custom repeat intervals with UILocalNotification. This has been asked before (see below) but only limited options are provided. The repeatInterval parameteris 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.

您不能使用UILocalNotification设置自定义重复间隔。之前已经问过这个问题(见下文),但只提供了有限的选项。在按repeatInterval参数是枚举类型和它不限于特定值

您不能将这些枚举相乘并获得这些间隔的倍数。您的应用中设置的本地通知不能超过 64 个。除非用户选择在通知触发时运行您的应用程序(他们可能不会运行它),否则您无法在通知触发后重新安排通知。

此处发布了对重复间隔乘数的请求。您可以为其添加注释。我建议向 Apple 提交错误报告或功能请求(网址?)。

Many other Stack Overflow answers confirm the claims in the quotes above. Visit the link to the full quoted answer, which contains a list of supporting answers.

许多其他 Stack Overflow 答案证实了上述引用中的说法。访问完整引用答案的链接,其中包含支持答案的列表。

A potential workaround for your case would be to schedule 3 local notifications. Set each one to fire at 6:28 AM, 12:28 PM, and 5:28 PM, respectively. Then, set the repeatIntervalof all 3 local notifications to .CalendarUnitWeekday.

您的情况的一个潜在解决方法是安排 3 个本地通知。分别将每个设置在上午 6:28、下午 12:28 和下午 5:28 触发。然后,将repeatInterval所有 3 个本地通知的 设置为.CalendarUnitWeekday