Проблема здесь в том, что для триггера установлено значение «Истина» для повторов.
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
Вам нужно будет установить для него значение false и повторно запускать этот метод каждый раз, когда вы хотите запланировать разные уведомление (другое содержимое)
Чтобы получать уведомление, когда приложение находится на переднем плане, вам необходимо реализовать метод willPresent
из UNUserNotificationCenterDelegate
Вот простой пример:
class ViewController: UIViewController {
private let userNotificaionCenter = UNUserNotificationCenter.current()
override func viewDidLoad() {
super.viewDidLoad()
// 1. Assign the delegate
userNotificaionCenter.delegate = self
scheduleNotification()
}
func scheduleNotification() {
// Same notification logic that you have
}
}
// 2. Implement the UNUserNotificationCenterDelegate
extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// This will allow the app to receive alerts and sound while in the foreground
completionHandler([.alert, .sound])
}
}