简体   繁体   English

如何使用 UNNotificationRequest 设置在每个月的最后一天触发本地通知?

[英]How to set a local notification to fire on the last day of each month using UNNotificationRequest?

I was trying to set up local notifications to fire on the last day of each month regardless of whether it's the 28th, 29th, 30th, or 31st.我试图将本地通知设置为在每个月的最后一天触发,无论是 28 日、29 日、30 日还是 31 日。

With the previous framework UILocalNotification I could specify the fireDate and repeatInterval and it worked.使用以前的框架UILocalNotification我可以指定fireDaterepeatInterval并且它起作用了。 But now that I'm updating the framework to UNNotofication , I tried to use UNCalendarNotificationTrigger and set the DateComponents ' day to 0 .但是现在我正在将框架更新为UNNotofication ,我尝试使用UNCalendarNotificationTrigger并将DateComponents的日期设置为0 However it didn't fire.然而它并没有火。 Anything I was doing wrong?我做错了什么吗? Thanks!谢谢!

The code is as below.代码如下。 nextTriggerDate is nil. nextTriggerDate为零。

var components = DateComponents()
components.hour = 8
components.minute = 0
components.day = 0

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
print(trigger.nextTriggerDate())

You can use the following code, next last day month is calculated after notification is shown.您可以使用以下代码,在显示通知后计算下一个最后一天的月份。

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge])
    { (granted, error) in
        // Enable or disable features based on authorization
    }
    let current = UNUserNotificationCenter.current()
    current.delegate = self
    setNotificationRequest()
}


func setNotificationRequest(offset: Int = 0) {
    let content = UNMutableNotificationContent()
    content.title = "Alarm"
    content.body = "Alarm for End of Month"
    content.sound = UNNotificationSound.default
    
    var dateCompo = DateComponents()
    dateCompo.day = lastDay(offset: offset)
    let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateCompo, repeats: true)
    let request = UNNotificationRequest(identifier: "alarm-id", content: content, trigger: trigger)
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request)
}
func lastDay(offset: Int) -> Int {
    let cal = Calendar.current
    let m = cal.component(.month, from: Date()) + 1
    let y = cal.component(.year, from: Date())
    var comps = DateComponents(calendar: cal, year: y, month: m)
    comps.setValue(m + 1, for: .month)
    comps.setValue(0, for: .day)
    let date = cal.date(from: comps)!
    return cal.component(.day, from: date)
} }


extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // offset is set to 1 as we want to get last of next month now.
    setNotificationRequest(offset: 1)
    completionHandler([.badge, .sound])
} }

I have tested it, working fine for me.我已经测试过了,对我来说工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM