加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:43:37 所属栏目:百科 来源:网络整理
导读:我一直在这些论坛和其他网站上,我不断得到一些不加分的答案.基本上,我想创建一个通知,例如,每个工作日在上午6:28,下午12:28和下午5:28触发. 我有一些解决方案,但我真的不确定去哪里.我是否正确地设置了这个?任何帮助表示赞赏. let notification: UILocalNot
我一直在这些论坛和其他网站上,我不断得到一些不加分的答案.基本上,我想创建一个通知,例如,每个工作日在上午6:28,下午12:28和下午5:28触发.

我有一些解决方案,但我真的不确定去哪里.我是否正确地设置了这个?任何帮助表示赞赏.

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

在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 07001. This has been asked before (see below) but
only limited options are provided. The 07002
is an enum type and it limited to 07003.

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 07004.
You can add comments to it. I suggest filing a bug report or feature
request (url?) with Apple.

许多其他Stack Overflow答案确认了上述引文中的声明.访问完整引用答案的链接,其中包含支持答案列表.

您的案例的潜在解决方法是安排3个本地通知.将每一个分别设置为分别在上午6:28 AM,下午12:28和下午5:28开火.然后,将所有3个本地通知的repeatInterval设置为.CalendarUnitWeekday.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读