如何让本地通知在每月的第一天每天重复 - swift

How to make local notification repeat every day on the first of the month - swift

我想知道如何在每个月的第一天重复 swift 中的本地通知。所以在一月一日,提醒一下。 2 月 1 日,同样的提醒,依此类推。最好使用日期组件。

  1. 首先我们需要获取默认值 "UNUserNotificationCenter"。
  2. 创建"UNNotificationContent".
  3. 创建 "UNCalendarNotificationTrigger".
  4. 创建"UNNotificationRequest".
  5. 将 "UNNotificationRequest" 添加到 "UNUserNotificationCenter"。
let center =  UNUserNotificationCenter.current()

//create the content for the notification
let content = UNMutableNotificationContent()
content.title = " Title"
content.subtitle = "SubTitle"
content.body = "jvsvsvasvbasbvfasfv"
content.sound = UNNotificationSound.default

var dateComp = DateComponents()
dateComp.month = 1;
dateComp.day = 1;
dateComp.hour = 00;
dateComp.minute = 00;

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComp, repeats: true)

//create request to display
let request = UNNotificationRequest(identifier: "ContentIdentifier", content: content, trigger: trigger)

//add request to notification center
center.add(request) { (error) in
    if error != nil {
        print("error \(String(describing: error))")
    }
}

使用可以参考这个link更多信息