Apple Watch通知有效负载

时间:2015-03-31 04:04:00

标签: ios swift push-notification watchkit

我想在Apple Watch通知中添加值(当前屏幕使用硬编码数据):

enter image description here

我要添加的值适用于以下字段:“Amount”,“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."
}

1 个答案:

答案 0 :(得分: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. 点击底部的重复方案,并提供自定义名称,例如' NOTIFICATION-Mywatchkitapp'等...

    3. 然后,选择WatchKit Interface to Dynamic Notification,Notification Payload到PushNotificationPayload.apns文件并运行此目标。