这里是我使用的通知有效载荷:
{
"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