gpt4 book ai didi

ios - 有关 Swift 中等待批准的计划通知的问题

转载 作者:行者123 更新时间:2023-12-02 03:00:38 26 4
gpt4 key购买 nike

我创建了一项功能,可以在您设置的特定时间和星期几发出警报。

func scheduleNotification() {

let center = UNUserNotificationCenter.current()
​ let hour = 6
​ let minute = 40
​ let weekdays = [2,3,4] // mon, tue, wed
​ let content = UNMutableNotificationContent()

content.title = "Fire!!"
content.body = "test.!"
content.badge = 1
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default

for weekday in weekdays {
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = weekday

let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats: true)

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
center.getPendingNotificationRequests { (requests) in
for request in requests {
print("trigger : \(request.trigger)")
}
}

UNUserNotificationCenter.current().add(request) { (error) in

}
}
}

正如您在上面的代码中看到的,您要重复的日子是星期一、星期二和星期三。

工作日 = [2,3,4]

之后,我想使用 [2,3,4] 数组上的 for 语句创建一个在星期一、星期二和星期三响起的警报。

但是闹钟没有按我想要的那样响起。

我使用 getPendingNotificationRequests 方法将计划的警报列表打印到控制台。

trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x2830567a0> {
Hour: 6
Minute: 40
Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283241920; dateComponents: <NSDateComponents: 0x283056900> {
Hour: 6
Minute: 40
Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832416a0; dateComponents: <NSDateComponents: 0x2830556f0> {
Hour: 6
Minute: 40
Weekday: 3, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x283056900> {
Hour: 6
Minute: 40
Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832426e0; dateComponents: <NSDateComponents: 0x283054c40> {
Hour: 6
Minute: 40
Weekday: 3, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832413c0; dateComponents: <NSDateComponents: 0x283056930> {
Hour: 6
Minute: 40
Weekday: 4, repeats: YES>)

我想要的是三个触发器,Weekday一一存储在[2,3,4]数组中。

但是,创建了六个触发器,并且 Weekday 的顺序存储不正确。

我想问两个问题。

  1. 如果你看一下上面代码的日志,如果你有一系列Weekday存储为2的触发器,它们是一个一个执行的吗?例如,如果是星期一,那么今天听起来一次,下周一听起来一次吗?

  2. 为什么警报存储的错误如此之大,我该如何解决这个问题?

最佳答案

您获得六个触发器的原因是,在 for 循环(for weekday in weekdays)中,您将每个触发器添加两次。每个触发器在 center.add(request) 行添加一次,并在 UNUserNotificationCenter.current().add(request) { (error) in 行再次添加。您可以通过将 for 循环更新为如下所示来解决此问题:

for weekday in weekdays {
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = weekday

let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats: true)

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

center.current().add(request) { (error) in
if error != nil {
print(error.localizedDescription)
}
}
}

您可能只想在 for 循环完成之后打印触发器,而不是在 for 循环内打印触发器。所以我会在 for 循环之后添加以下代码:

func scheduleNotification() {

...

for weekday in weekdays {

...

}

//This following code was originally within your for loop, but should probably execute after the for loop instead
//The console should look a lot cleaner after doing so
center.getPendingNotificationRequests { (requests) in
for request in requests {
print("trigger : \(request.trigger)")
}
}
}

关于ios - 有关 Swift 中等待批准的计划通知的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449164/

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