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

时间:2018-08-30 15:56:18

标签: ios swift xcode

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

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.")

}

1 个答案:

答案 0 :(得分:0)

这是因为相同的标识符

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

因此请为每个计划的通知更改