有人可以通过在长 View 本地通知中添加按钮来告诉我吗?我对 watch 套件和通知都是新手。
长视功能正常。我正在设置 UILocalNotification
在我的主机应用程序中,设置 alertBody、category、userInfo 等并将其发送出去。
在我的通知 Controller 中,我在 didReceiveLocalNotification
中设置了所有内容它工作正常。
从我的研究看来,我应该以某种方式将按钮添加到通知中并使用方法 handleActionWithIdentifier: forLocalNotification
但我不清楚如何做到这一点。
我正在用Objective-C编写。谢谢
最佳答案
这是我正在使用的通知有效负载:
{
"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"
}
],
}
创建自定义通知子类并覆盖以下方法,不要忘记在相应的 Storyboard Controller 中设置该子类:
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)
}
之后,将出现标题为“回复”的通知自定义按钮。当您按下它时,它将启动 watch 应用程序主界面 Controller 并调用
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)
}
}
}
}
}
附言
Context
是我自己的用于在 WKInterfaceController
之间传递数据的类的子类希望对你有帮助!)
关于ios - 如何将按钮添加到 Apple WatchKit Long Look 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329304/