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

Pan*_*ngu 3 nsnotificationcenter ios swift swift3

我试图通过尝试触发每日通知来测试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)
Run Code Online (Sandbox Code Playgroud)

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

指定false以在触发器触发后取消计划通知.指定true会导致重复重新安排通知.

假设我在今天下午6:56触发通知.下午6:56,我看到了预期的本地通知.

当我尝试通过设置 - >日期和时间手动将日期和时间更改为下午6:55的4/11/2017时,下午6:56到处滚动,我根本看不到通知.

它不能以这种方式测试吗?

我没有尝试在指定时间实际等待第二天查看是否会弹出另一个通知,但我很好奇为什么这种测试方式不起作用?

谢谢.

Ale*_*eod 5

你打电话repeatsDate一个月和一个月.因此,根据法律规定,该日期将永远不会再发生到明年,因此第二天不会发送通知.通知的触发器必须仅包含小时和分钟值,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)
Run Code Online (Sandbox Code Playgroud)