我创建了一个函数,它会在特定的小时和分钟向用户发送指定文本的通知。问题是,当我调用该函数两次(尝试发送两个通知)时,它只运行第二个通知。基本上,我需要能够多次调用该函数以在不同时间获得多个通知。我的猜测是第二次称它为先前的覆盖。无论如何,我可以修复/完成这个?这是我的代码:
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)
因此,请为每个预定通知更改它