gpt4 book ai didi

ios - 我有一个场景,我想添加3天/5天吃药的通知。根据用户要求进行提醒

转载 作者:行者123 更新时间:2023-11-29 05:58:02 24 4
gpt4 key购买 nike

这是更新后的代码,为此创建了一个函数并在 viewDidLoad 中调用。我试图通过循环显示它四天,包括今天。

 func scheduleNotification(day: Int) {

var date = DateComponents()
date.year = 2019
date.month = 3
date.day = day
date.hour = 12
date.minute = 30

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Hi User"
content.body = "Take Medicine"
content.sound = UNNotificationSound.default


let request = UNNotificationRequest(identifier: "textNotification\(day)", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}





override func viewDidLoad()
{
super.viewDidLoad()

for index in 1...4 {
scheduleNotification(day: index)
}
}

最佳答案

scheduleNotification中,您正在调用removeAllPendingNotificationRequests()

这将删除所有先前安排的通知。

由于您调用 scheduleNotification 4 次,前三个通知中的每一个都将被后续调用 scheduleNotification 取消。

仅保留最终通知。

您应该删除对 removeAllPendingNotificationRequests() 的调用。如果需要,您可以在 for 循环之前调用此函数。

关于ios - 我有一个场景,我想添加3天/5天吃药的通知。根据用户要求进行提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54955589/

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