ios - 安排 iOS 本地推送通知

标签 ios swift push-notification ios10 uilocalnotification

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 - 安排 iOS 本地推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49029358/

相关文章:

ios - 在按钮上切换图像单击 iOS

ios - 为什么我的 AdMob 插页式广告在 Xcode Simulator 中运行良好,但在我的测试设备上却不行?

iphone - 如何从 Xcode 中删除运行脚本阶段

ios - AVCaptureVideoPreviewLayer 未填满屏幕

iOS 7 : UINavigationController pulls down UIScrollview

android - GCM,用户通知,同一设备上的两个 GCM 感知应用程序,当 GCM 消息与通知 key 一起发送时触发哪个应用程序?

java - 创建 Java 桌面通知

ios - 将渐变效果应用于模糊 View

swift - super.init 初始化 self 之前在属性访问 'self' 中使用 'model' super.init(brand : brand, model: model)

android - 如何在 service worker 中播放声音甚至在 chrome 中振动?