安排iOS本地推送通知
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安排iOS本地推送通知相关的知识,希望对你有一定的参考价值。
let center = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.alert, .sound];
center.requestAuthorization(options: options) { (granted, error) in
if !granted {
print("Something went wrong")
}
}
center.getNotificationSettings { (settings) in
if settings.authorizationStatus != .authorized {
// Notifications not allowed
}
}
let content = UNMutableNotificationContent()
content.title = "Good Morning"
content.body = "wake up"
content.sound = UNNotificationSound.default()
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
let identifier = "localNotification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let _ = error {
LOGG("Something went wrong")
}
})
上面的代码,在早上7点触发推送通知。我如何使用相同的代码在晚上9点或12点设置通知。谢谢。
答案
尝试在晚上9点开火
var dateInfo = DateComponents()
dateInfo.hour = 21
dateInfo.minute = 0
以上是关于安排iOS本地推送通知的主要内容,如果未能解决你的问题,请参考以下文章