2015-03-31 181 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