ios - 如何将按钮添加到 Apple WatchKit Long Look 通知

标签 ios uilocalnotification watchkit

有人可以通过在长 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/

相关文章:

ios - "Myapp would like to send you push notifications"消息没有出现在我的应用程序中,但它仍然允许出现推送通知

iphone - 是否可以在发送通知时停止播放 UILocalNotification 的声音?

watchkit - watchOS 2.0 运行 watchOS 1.0 应用程序?

iOS 4 : Is it possible to show Local Notifications during a Telephony Call?

ios - 如何在iOS中识别不同连接的 watch

ios - Xcode:无法在 Apple Watch 模拟器 ("Will Install Watch App timed out"上安装应用程序)

ios - 如何将我的 UIImageView 内容保存到我的画廊? ( swift )

ios - SpriteKit - 如何正确暂停和恢复应用程序

ios - 将 Google 帐户登录集成到 iOS 应用程序中

iphone - 应用程序 didReceiveLocalNotification 未触发 iOS7