這裏是我使用的通知有效載荷:
{
"aps": {
"alert": {
"body": "Hi! How you doing? This is a test message. Press 'Reply' button to reply. Thanks!",
"title": "Petya"
},
"category": "newMessage"
},
"WatchKit Simulator Actions": [
{
"title": "Reply",
"identifier": "replyPressed"
}
],
}
創建自定義通知子類並覆蓋下面的方法,不要忘記設置該子類中相應的情節串連圖板控制器:
override func didReceiveRemoteNotification(remoteNotification: [NSObject : AnyObject], withCompletion completionHandler: (WKUserNotificationInterfaceType) -> Void) {
if let aps = remoteNotification["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let title = alert["title"] as? String {
titleLabel.setText(title)
}
if let body = alert["body"] as? String {
bodyLabel.setText(body)
}
}
}
completionHandler(WKUserNotificationInterfaceType.Custom)
}
之後,在您的通知自定義按鈕標題'回覆'將出現。當你按下它時,它會啓動手錶應用程序主界面控制器,並調用handleActionWithIdentifier:localNotification:
或handleActionWithIdentifier:remoteNotification:
,這取決於您收到的通知類型。你必須這樣覆蓋它:
override func handleActionWithIdentifier(identifier: String?, forRemoteNotification remoteNotification: [NSObject : AnyObject]) {
println("identifier: \(identifier)")
println("remoteNotification: \(remoteNotification)")
if identifier == "replyPressed" {
if let aps = remoteNotification["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let title = alert["title"] as? NSString {
let context = Context()
context.object = title
pushControllerWithName(kTCChatRoomControllerIdentifier, context: context)
}
}
}
}
}
P.S. Context
是我自己爲WKInterfaceController
的子類之間傳遞數據
希望這將幫助你上課!)
謝謝,我試圖弄清楚迅捷。當你說「通知有效載荷」是你用JSON加載UILocalNotification,然後檢查'didReceiveLocalNotification'方法中的JSON? – MayNotBe
您可以從Xcode-> New File-> Apple Watch-> Notification Simulation File創建臨時通知有效載荷文件(基本上是JSON文件)。 –
@MayNotBe詢問localNotifications不是remoteNotifications,localNotification不支持Payload文件 – EridB