gpt4 book ai didi

ios - Swift 4 重复本地通知

转载 作者:行者123 更新时间:2023-11-28 07:33:15 25 4
gpt4 key购买 nike

我正在尝试发出一个通知,该通知将在每天的同一时间(上午 7:00)触发。如果我使用 UNTimeIntervalNotificationTrigger 但不是我真正想要的 UNCalendarNotificationTrigger ,我能够获得触发通知。这是我目前拥有的:

在 AppDelegate.swift 中我有:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (didAllow, error) in
if error != nil {
print(error as Any)
}
}

return true
}

我还记得导入 UserNotifications

当用户按下 ViewController.swift 中的按钮时,它会运行:

let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

let triggerTime = DateComponents.init(hour: 7, minute: 0, second: 0)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

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

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil

能否请您向我解释这里出了什么问题,以及为什么 UNTimeIntervalNotificationTrigger 有效但 UNCalendarNotificationTrigger 无效?

最佳答案

我不确定它是否能解决您的问题,但您是直接从 init 设置 DateComponent。我建议您通过初始化一个空的 DateComponent 并仅根据需要设置时间来完成此操作。

那么,你可以尝试这样做吗:

let content = UNMutableNotificationContent()
content.title = "Do your daily review"
content.badge = 1

var triggerTime = DateComponents()
triggerTime.hour = 7

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)

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

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

我自己做了,效果很好。

希望对您有所帮助。

关于ios - Swift 4 重复本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54035853/

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