我正在使用 WatchOS 3 beta 并尝试在手表上启动本地通知。该界面只是一个按钮,它在下面的代码中调用“buttonPushed”方法。该应用程序运行良好,但我从未收到通知。应用程序结构是 Xcode 8 中 WatchKit 应用程序的默认结构。
此代码位于 WatchKit 扩展的 InterfaceController.swift 文件中
我错过了一些完全明显的东西吗?
@IBAction func buttonPushed() {
sendMyNotification()
}
func sendMyNotification(){
if #available(watchOSApplicationExtension 3.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "REMINDER_CATEGORY"
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
center.add(request ,withCompletionHandler: nil)
} else {
// Fallback on earlier versions
}
}