2015-03-31 118 views
5

我要值添加到蘋果手錶通知(當前屏幕採用硬編碼數據):Apple關注通知載荷

enter image description here

我想補充的值是這些字段:「金額」 ,「At」和「When」。我如何添加從PushNotificationPayload.apns文件中獲取這些值並在通知中顯示它?

這是PushNotificationPayload.apns文件:

{ 
"aps": { 
    "alert": { 
     "body": "New Transaction\n\n", 
     "title": "Optional title" 
    }, 
    "category": "newTransactionCategory" 
}, 

"WatchKit Simulator Actions": [ 
           { 
           "title": "Details", 
           "identifier": "transactionDetailsButtonAction" 
           } 
           ], 

"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App." 
} 

回答

4

這些步驟,

  • 創建一個新的類,它是WKUserNotificationInterfaceController一個子類。

  • 從故事板中,選擇動態界面場景進行通知(如果您尚未創建它,請在靜態場景的屬性檢查器中啓用「有動態界面」),並在Identity Inspector中設置上述創建的自定義類。

  • 現在修改PushNotificationPayload.apns如下文件內容,

    { 
        "aps": { 
         "alert": { 
          "body": "New Transaction\n\n", 
          "title": "Optional title" 
         }, 
         "category": "newTransactionCategory" 
        }, 
    
        "WatchKit Simulator Actions": [ 
                { 
                "title": "Details", 
                "identifier": "transactionDetailsButtonAction" 
                } 
                ], 
    
        "Amount": "USD 20", 
        "At": "Mc Donalds", 
        "When": "Today", 
    } 
    
  • 當接收遠程通知,這種方法會在您的自定義通知接口類被調用,您將收到自定義鍵您需要使用字典'remoteNotification'來設置標籤的文本。

    -(void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler { 
        NSLog(@"remoteNotification Dictionary %@",remoteNotification); 
    
        completionHandler(WKUserNotificationInterfaceTypeCustom); 
    } 
    
  • 最後是調試:

    1. 底部頂部選擇你的目標,然後選擇編輯計劃

    2. 點擊複製方案,給喜歡「你的自定義名稱通知 - Mywatchkitapp'等...

    3. 然後,選擇WatchKit接口動態通知,通知有效負載到您的PushNo tificationPayload.apns文件併爲此目標運行。

+0

喜@ DH14-S L,我想試試您的解決方案。不過目前我遇到運行自定義長外觀通知的問題。我已經在這裏發佈錯誤:http://stackoverflow.com/questions/29341051/how-to-avoid-this-error-took-too-long-to-show-custom-notification-falling-bac/29345228# 29345228 – user1872384 2015-03-31 07:14:30

+0

確保Storyboard和Payload中的'category'鍵是正確的。然後檢查你是否在'willActivate'上寫了任何長時間運行的代碼。 – Dhawal 2015-03-31 07:37:39

+0

不幸的是,它仍然沒有解決錯誤。 :(即使創建一個新的項目,並設置一個新的watchkit目標與啓用自定義通知也會給這個錯誤以及...這是如此令人沮喪... – user1872384 2015-03-31 07:41:38