如何在每个月的第一天每天重复发送本地通知-Swift

时间:2019-09-10 03:54:51

标签: swift xcode usernotifications

我想知道如何在每个月的第一天快速重复发送本地通知。因此,在1月1日提醒您。 2月1日,同样的提醒,依此类推。最好使用日期组件。

1 个答案:

答案 0 :(得分:0)

  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更多信息。