首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 10 UserNotification框架。日历触发器

iOS 10 UserNotification框架。日历触发器
EN

Stack Overflow用户
提问于 2017-04-29 02:53:13
回答 1查看 464关注 0票数 1

我使用的是iOS 10.3.2 Beta4和Xcode8.3.2。我尝试每天同时发出通知,但通知似乎从未发出过。如果我不使用日历触发器,而使用时间间隔触发器,它们将按预期工作。

以下是我的代码

代码语言:javascript
复制
let content = UNMutableNotificationContent()
    content.title = "Daily Notification"
    content.subtitle = ""
    content.body = NSLocalizedString("Hello I am a notification", comment: "Comment")
    content.badge = 1
    content.sound = UNNotificationSound.default()

    let calendar = Calendar.current
    var dateComponents = calendar.dateComponents([.day, .month, .year, .hour], from: Date())
    dateComponents.hour = 14
    dateComponents.minute = 55

    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    NSLog("Setting daily notification - \(dateComponents)")

    let request = UNNotificationRequest(identifier: "dailyWarning", content: content, trigger: trigger)

    center.add(request, withCompletionHandler: { error in

        if error != nil {
            NSLog("Error occurred")
        } else {
            NSLog("Request added successfully: %@", request)
        }
    })
EN

回答 1

Stack Overflow用户

发布于 2017-05-01 21:42:01

尝试像这样创建触发器:

代码语言:javascript
复制
var dateComponents = DateComponents()
dateComponents.hour = 14
dateComponents.minute = 55
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) // Setting repeats to true will trigger the notification daily

有关为什么只提供hourminutes就足够了的更多信息,请参阅文档found here中的清单1中的解释。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43687090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档