【问题标题】:How to make local notification repeat every day on the first of the month - swift如何在每月的第一天每天重复本地通知 - swift
【发布时间】:2020-01-11 19:12:00
【问题描述】:

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

【问题讨论】:

    标签: swift xcode usernotifications


    【解决方案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更多信息。

    【讨论】:

    • @EvanC 重复标志在 UNCalendarNotificationTrigger 中设置为 true 对吗?所以它应该重复我猜。您可以通过在 iPhone 中手动更改日期来进行测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2012-02-24
    • 2013-05-11
    • 2020-10-02
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多