mac*_*zer 6 notifications ios watchos-3
我正在使用WatchOS 3测试版并尝试在手表上发起本地通知.界面只是一个按钮,它在下面的代码中调用"buttonPushed"方法.该应用程序运行正常但我从未收到通知.对于WatchKit应用程序,应用程序结构是Xcode 8的默认结构.
此代码位于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
}
}
Run Code Online (Sandbox Code Playgroud)
根据此。您应该每次为请求指定一个唯一的新标识符。
矿:
let id = String(Date().timeIntervalSinceReferenceDate)
let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
Run Code Online (Sandbox Code Playgroud)