我正在尝试通过触发每日通知来测试 iOS 10 中的本地通知。
我正在使用以下示例项目:通知UI-演示 https://github.com/appcoda/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
from false to true每天重复,因为根据描述的文档repeats
范围:
指定 false 可在触发器触发后取消安排通知。
指定 true 会导致通知被重复重新安排。
假设我在今天 2017 年 4 月 10 日下午 6:56 触发了一条通知。下午 6:56,我如预期看到了本地通知。
当我尝试通过“设置”->“日期和时间”手动将日期和时间更改为 2017 年 4 月 11 日下午 6:55 时,一旦下午 6:56 到来,我根本看不到任何通知。
难道就不能这样测试吗?
我没有尝试实际等待第二天的指定时间来查看是否弹出另一个通知,但我很好奇为什么这种测试方式不起作用?
Thanks.