gpt4 book ai didi

swift - iOS 10 用户通知框架。日历触发器

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:18 31 4
gpt4 key购买 nike

我使用的是 iOS 10.3.2 Beta 4 和 xcode 8.3.2。我试图每天在同一时间触发通知,但通知似乎从未触发过。如果我不使用日历触发器并使用时间间隔触发器,它们将按预期工作。

这是我的代码

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

最佳答案

尝试像这样创建触发器:

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

有关为什么仅提供小时分钟 就足够的更多信息,请参阅文档中的上述 list 1 说明 here .

关于swift - iOS 10 用户通知框架。日历触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43687090/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com