Can someone please talk me through adding a button to a long-look local notification? I'm new to both the watch kit and notifications.
有人可以通过在长按本地通知中添加按钮来与我联系吗?我是手表套件和通知的新手。
The long-look is functioning correctly. I'm setting up a UILocalNotification
in my host app, setting the alertBody, category, userInfo, etc. and sending it off.
长期看起来功能正常。我正在我的主机应用程序中设置UILocalNotification,设置alertBody,category,userInfo等并将其发送出去。
In my notification controller I'm setting everything up in didReceiveLocalNotification
and it's working fine.
在我的通知控制器中,我在didReceiveLocalNotification中设置了所有内容,它运行正常。
It seems, from my research, that I'm supposed to somehow add the button to the notification and use the method handleActionWithIdentifier: forLocalNotification
but I'm unclear exactly how to do this.
从我的研究来看,似乎我应该以某种方式将按钮添加到通知并使用方法handleActionWithIdentifier:forLocalNotification但我不清楚如何做到这一点。
I'm writing in Objective-C. Thanks
我在写Objective-C。谢谢
2 个解决方案
#1
0
Here is notification payload I'm using:
这是我正在使用的通知有效负载:
{
"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"
}
],
}
Create custom notification subclass and override following method and don't forget to set that subclass in corresponding storyboard controller:
创建自定义通知子类并覆盖以下方法,不要忘记在相应的storyboard控制器中设置该子类:
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)
}
After that in your notification custom button with title 'Reply' will appear. And when you press it it will launch watch app main interface controller and call handleActionWithIdentifier:localNotification:
or handleActionWithIdentifier:remoteNotification:
, depends on kind of notification you received. You have to override it like that:
在您的通知中,将出现标题为“回复”的自定义按钮。当你按下它时,它将启动监视应用程序主界面控制器并调用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
is my own class for passing data between WKInterfaceController
's subclasses
附: Context是我自己的类,用于在WKInterfaceController的子类之间传递数据
Hope this will help you!)
希望对你有帮助!)
#2
0
The Notification Essentials section of the Apple Watch Programming Guide describes how to add action buttons to Long-Look notifications for local notifications.
Apple Watch编程指南的Notification Essentials部分介绍了如何为本地通知的Long-Look通知添加操作按钮。
Basically, you need to create a UIMutableUserNotificationAction for every button that you want to add to your Long-Look interface, along with a category for your local notification. This is done in the App Delegate of your iOS app, when you register for notifications. Listing 15-1 in that guide shows exactly how to do it. Then, in the storyboard for your Apple Watch app, set your notification's category to the category you just created. The next section of the guide, "Responding to Taps in Action Buttons," should hopefully tell you everything you need to know in order to either launch your WatchKit app, or execute a task in the background on the iPhone.
基本上,您需要为要添加到Long-Look界面的每个按钮创建一个UIMutableUserNotificationAction,以及本地通知的类别。当您注册通知时,这将在您的iOS应用程序的App Delegate中完成。该指南中的清单15-1显示了如何执行该操作。然后,在Apple Watch应用程序的故事板中,将通知的类别设置为刚刚创建的类别。本指南的下一部分“响应操作按钮中的按钮”应该有希望告诉您需要知道的一切,以便启动WatchKit应用程序,或在iPhone后台执行任务。
#1
0
Here is notification payload I'm using:
这是我正在使用的通知有效负载:
{
"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"
}
],
}
Create custom notification subclass and override following method and don't forget to set that subclass in corresponding storyboard controller:
创建自定义通知子类并覆盖以下方法,不要忘记在相应的storyboard控制器中设置该子类:
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)
}
After that in your notification custom button with title 'Reply' will appear. And when you press it it will launch watch app main interface controller and call handleActionWithIdentifier:localNotification:
or handleActionWithIdentifier:remoteNotification:
, depends on kind of notification you received. You have to override it like that:
在您的通知中,将出现标题为“回复”的自定义按钮。当你按下它时,它将启动监视应用程序主界面控制器并调用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
is my own class for passing data between WKInterfaceController
's subclasses
附: Context是我自己的类,用于在WKInterfaceController的子类之间传递数据
Hope this will help you!)
希望对你有帮助!)
#2
0
The Notification Essentials section of the Apple Watch Programming Guide describes how to add action buttons to Long-Look notifications for local notifications.
Apple Watch编程指南的Notification Essentials部分介绍了如何为本地通知的Long-Look通知添加操作按钮。
Basically, you need to create a UIMutableUserNotificationAction for every button that you want to add to your Long-Look interface, along with a category for your local notification. This is done in the App Delegate of your iOS app, when you register for notifications. Listing 15-1 in that guide shows exactly how to do it. Then, in the storyboard for your Apple Watch app, set your notification's category to the category you just created. The next section of the guide, "Responding to Taps in Action Buttons," should hopefully tell you everything you need to know in order to either launch your WatchKit app, or execute a task in the background on the iPhone.
基本上,您需要为要添加到Long-Look界面的每个按钮创建一个UIMutableUserNotificationAction,以及本地通知的类别。当您注册通知时,这将在您的iOS应用程序的App Delegate中完成。该指南中的清单15-1显示了如何执行该操作。然后,在Apple Watch应用程序的故事板中,将通知的类别设置为刚刚创建的类别。本指南的下一部分“响应操作按钮中的按钮”应该有希望告诉您需要知道的一切,以便启动WatchKit应用程序,或在iPhone后台执行任务。