我正在使用WatchOS 3測試版並嘗試在手錶上啓動本地通知。界面只是一個按鈕,在下面的代碼中調用「buttonPushed」方法。該應用運行良好,但我從來沒有收到通知。應用程序結構是Xcode 8對於WatchKit應用程序的默認設置。WatchOS中的本地通知3
此代碼是在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
}
}
也可以使用'讓創建一個唯一的標識的字符串ID = UUID.init()。uuidString' –
一旦有生命,它可能是兩個相同的UUID。 – Vladimir