在 iOS 中重复每日本地通知 10 手动设置时间和日期时不会触发?

Repeat Daily Local Notifications in iOS 10 does not get triggered when manually setting the Time and Date?

我正在尝试通过触发每日通知来测试 iOS 10 中的本地通知。

我正在使用以下示例项目: NotificationsUI-Demo

在应用程序中是以下代码之一:

let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

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

    let content = UNMutableNotificationContent()
    content.title = "Tutorial Reminder"
    content.body = "Just a reminder to read your tutorial over at appcoda.com!"
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "myCategory"

    if let path = Bundle.main.path(forResource: "logo", ofType: "png") {
        let url = URL(fileURLWithPath: path)

        do {
            let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
            content.attachments = [attachment]
        } catch {
            print("The attachment was not loaded.")
        }
    }

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

    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("Uh oh! We had an error: \(error)")
        }
    }

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

我将 repeats 的值从 false 更改为 true 以每天重复,因为根据描述 repeats参数:

Specify false to unschedule the notification after the trigger fires. Specifying true causes the notification to be rescheduled repeatedly.

假设我在 6:56pm 触发了今天 2017 年 4 月 10 日的通知。在 6:56pm,我看到了预期的本地通知。

当我尝试通过“设置”->“日期和时间”将日期和时间手动更改为 4/11/2017 6:55pm 时,一旦 6:56pm 滚动,我没有看到通知完全没有。

难道不能这样测试吗?

我没有试过真的等到第二天的指定时间再弹出通知,但我很好奇为什么这种测试方式不起作用?

谢谢。

您正在 Date 呼叫 repeats,日期和月份。因此,这个日期要到明年才会再次出现。因此,第二天不会发送通知。您的通知触发器必须仅包含使用 DateComponents 的小时和分钟值,以允许通知每天重复:

var date = DateComponents()
date.hour = 11
date.minute = 41

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNNotificationContent()

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

试试这行代码:

let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Title"
notificationContent.body = "Body Message"

var date = DateComponents()
date.hour = 11
date.minute = 30
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)


let notificationRequest = UNNotificationRequest(identifier: "\(NSDate().timeIntervalSince1970)", content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
    if let error = error
    {
        let errorString = String(format: NSLocalizedString("Unable to Add Notification Request %@, %@", comment: ""), error as CVarArg, error.localizedDescription)
        print(errorString)
    }
}