尝试触发 UILocalNotification 不起作用

Trying to fire UILocalNotification not working

我正在使用下面的代码创建通知,但不知为何它没有触发。我真的只是希望它在我打开某个视图控制器时触发,但到目前为止没有触发。我的代码有什么问题?

func notify() {
    let notification = UILocalNotification()
    notification.fireDate = Date()
    notification.repeatInterval = .minute
    notification.alertBody = "The alert body"
    notification.alertAction = "enter text here"
    UIApplication.shared.scheduleLocalNotification(notification)
}

我在应用委托中也有这一行:

application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))

我认为这会失败的一个原因是因为 notification.fireDate = Date() 会将 fireDate 设置为当前时间,当管理 UserNotifications 的(单独的)进程收到警报时,这将是过去的时间你想发出通知。考虑搬到 UNNotifications

我将触发日期更改为将来的某个时间(当时是下午 6 点,我使用 calendarComponents 将其更改为 06:03,然后它似乎起作用了。通知被触发了 :)