gpt4 book ai didi

ios - 通过 Xcode 中 Swift 中的函数运行多个通知?

转载 作者:行者123 更新时间:2023-11-30 11:11:59 25 4
gpt4 key购买 nike

我创建了一个函数,它将在特定的时间和分钟向用户发送带有指定文本的通知。问题是,当我调用该函数两次(尝试发送两个通知)时,它只运行第二个通知。本质上,我需要能够多次调用该函数以在不同时间获取多个通知。我的猜测是第二次调用它会覆盖之前的调用。无论如何我可以解决/完成这个问题吗?这是我的代码:

func notification(hour: Int, minute: Int, text: String){

let content = UNMutableNotificationContent()
content.title = "Example"
content.body = text
content.sound = UNNotificationSound.default()

var date = DateComponents()
date.hour = hour
date.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)

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

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

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

notification(hour: 12, minute: 5, text: "It is 12:05.")
notification(hour: 12, minute: 10, text: "It is 12:10.")

}

最佳答案

这是因为相同的标识符

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

因此请针对每个预定通知进行更改

关于ios - 通过 Xcode 中 Swift 中的函数运行多个通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101148/

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