【问题标题】:How I can set a notification which UserNotifications Framework如何设置 UserNotifications Framework 的通知
【发布时间】:2016-07-07 18:05:56
【问题描述】:

我可以设置一个时间间隔的通知,但我不知道如何在特定的时间和日期进行,我试试这个但不起作用

let center = UNUserNotificationCenter.current()

func notificationSender(){

    center.requestAuthorization([.sound, .alert]) {
        (granted, error) in
        // We can register for remote notifications here too!
    }

    var date = DateComponents()
    date.hour = 13
    date.minute = 57

    let trigger = UNCalendarNotificationTrigger.init(dateMatching: date , repeats: false)

    let content = UNNotificationContent()
    // edit your content

    let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)

center.add(通知) }

需要在每周一和周五下午 3 点重复通知

【问题讨论】:

    标签: swift swift3 calendar ios10 uilocalnotification


    【解决方案1】:

    你快到了。你要做的事情不多了。

    您缺少的内容如下:

    • 您应该将 weekday 添加到您的日期组件中,1 用于周日,因此您需要将其设置为 2 用于周一通知,6 用于周五通知
    • 如果需要重复,则需要在UNCalendarNotificationTrigger 中将参数repeats 设置为true

    这是一个例子。

    // Create date components that will match each Monday at 15:00
    var dateComponents = DateComponents()
    dateComponents.hour = 15
    dateComponents.minute = 0
    dateComponents.weekday = 2 // Monday
    
    // Create a calendar trigger for our date compontents that will repeat
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
                                                repeats: true)
    
    // Create the content for our notification
    let content = UNMutableNotificationContent()
    content.title = "Foobar"
    content.body = "You will see this notification each monday at 15:00"
    
    // Create the actual notification
    let request = UNNotificationRequest(identifier: "foo",
                                        content: content,
                                        trigger: trigger)
    
    // Add our notification to the notification center
    UNUserNotificationCenter.current().add(request)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      相关资源
      最近更新 更多