2015-05-19 68 views
2

有人可以通過添加一個按鈕到長時間的本地通知來討論我嗎?我對手錶套件和通知都很陌生。如何將按鈕添加到Apple WatchKit長時間看通知

長外觀運行正常。我在我的主機應用程序中設置了一個UILocalNotification,設置alertBody,category,userInfo等並將其發送出去。

在我的通知控制器中,我將所有設置都設置爲didReceiveLocalNotification,它工作正常。

從我的研究看來,我應該以某種方式將按鈕添加到通知並使用方法handleActionWithIdentifier: forLocalNotification,但我不清楚如何執行此操作。

我正在寫Objective-C。由於

回答

0

這裏是我使用的通知有效載荷:

{ 
    "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的子類之間傳遞數據

希望這將幫助你上課!)

+0

謝謝,我試圖弄清楚迅捷。當你說「通知有效載荷」是你用JSON加載UILocalNotification,然後檢查'didReceiveLocalNotification'方法中的JSON? – MayNotBe

+0

您可以從Xcode-> New File-> Apple Watch-> Notification Simulation File創建臨時通知有效載荷文件(基本上是JSON文件)。 –

+0

@MayNotBe詢問localNotifications不是remoteNotifications,localNotification不支持Payload文件 – EridB

0

蘋果觀察編程指南的Notification Essentials部分介紹如何添加動作按鈕長期尋找本地的通知通知。

基本上,您需要爲要添加到Long-Look界面的每個按鈕以及用於本地通知的類別創建一個UIMutableUserNotificationAction。當您註冊通知時,在您的iOS應用程序的應用程序委託中完成此操作。該指南中的清單15-1顯示瞭如何執行此操作。然後,在Apple Watch應用程序的故事板中,將通知的類別設置爲您剛剛創建的類別。本指南的下一部分「響應按鈕的點擊」應該有希望告訴您需要知道的一切,以便啓動WatchKit應用程序或在iPhone上執行後臺任務。