【问题标题】:iOS 10 UserNotification Framework. Calendar TriggeriOS 10 用户通知框架。日历触发器
【发布时间】:2017-04-28 18:53:12
【问题描述】:

我使用的是 iOS 10.3.2 Beta 4 和 xcode 8.3.2。我试图每天在同一时间触发通知,但通知似乎从未触发。如果我不使用日历触发器并使用时间间隔触发器,它们会按预期工作。

这是我的代码

let content = UNMutableNotificationContent()
    content.title = "Daily Notification"
    content.subtitle = ""
    content.body = NSLocalizedString("Hello I am a notification", comment: "Comment")
    content.badge = 1
    content.sound = UNNotificationSound.default()

    let calendar = Calendar.current
    var dateComponents = calendar.dateComponents([.day, .month, .year, .hour], from: Date())
    dateComponents.hour = 14
    dateComponents.minute = 55

    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    NSLog("Setting daily notification - \(dateComponents)")

    let request = UNNotificationRequest(identifier: "dailyWarning", content: content, trigger: trigger)

    center.add(request, withCompletionHandler: { error in

        if error != nil {
            NSLog("Error occurred")
        } else {
            NSLog("Request added successfully: %@", request)
        }
    })

【问题讨论】:

    标签: swift ios10 unusernotificationcenter


    【解决方案1】:

    尝试像这样创建触发器:

    var dateComponents = DateComponents()
    dateComponents.hour = 14
    dateComponents.minute = 55
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) // Setting repeats to true will trigger the notification daily
    

    有关为什么只提供hourminutes 就足够的更多信息,请参阅here 文档中的上述清单1 说明。

    【讨论】:

    • 使用这样的触发器似乎效果更好 UNCalendarNotificationTrigger.init(dateMatching: date, repeats: true)。
    • 但我肯定会奖励你积分。感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    相关资源
    最近更新 更多