Sim*_*pic 5 notifications swift3 ios10 ios10.2
我一直致力于在 iOS 10 中为我的应用程序设置本地通知,但在模拟器中测试时,我发现通知可以成功安排,但在安排的时间到来时永远不会真正出现。这是我一直在使用的代码:
let UNcenter = UNUserNotificationCenter.current()
UNcenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
// Enable or disable features based on authorization
if granted == true {
self.testNotification()
}
}
Run Code Online (Sandbox Code Playgroud)
然后它运行此代码(假设日期/时间是将来的时间):
func testNotification () {
let date = NSDateComponents()
date.hour = 16
date.minute = 06
date.second = 00
date.day = 26
date.month = 1
date.year = 2017
let trigger = UNCalendarNotificationTrigger(dateMatching: date as DateComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "TestTitle"
content.body = "TestBody"
content.subtitle = "TestSubtitle"
let request = UNNotificationRequest(identifier: "TestID", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("error: \(error)")
} else {
print("Scheduled Notification")
}
}
}
Run Code Online (Sandbox Code Playgroud)
从这段代码中,它将始终打印“预定通知”,但是当应该触发通知时,它永远不会触发。我一直无法找到任何解决办法。
| 归档时间: |
|
| 查看次数: |
5319 次 |
| 最近记录: |