0

我在 macOS 应用程序中使用以下代码创建了一个 UNNotificationRequest

let content = UNMutableNotificationContent()
        
content.title = "Welcome"
content.body = "body"
content.sound = UNNotificationSound.default
                                
var dateComponent = DateComponents()
        
dateComponent.year = 2021
dateComponent.day = 24
dateComponent.hour = 12
dateComponent.minute = 27
        
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
       
let request: UNNotificationRequest = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        
UNUserNotificationCenter.current().add(request) { (error) in
}

我希望在 2021 年每个月的 24 日触发通知。

当通知被触发时,它会每秒生成重复的重复通知。如果我将 datecomponents 中的 year 属性设为 nil,则它不会生成重复的重复通知。

但我需要根据我的要求设置年份属性。我在示例 iOS 应用程序中尝试了相同的代码。它不会生成重复的通知。它仅在 macOS 应用程序中生成。

如果我在 datecomponents 中设置 year 属性,不生成重复重复通知的解决方案是什么?

4

0 回答 0