Ios Apple Watch通知有效负载

Ios Apple Watch通知有效负载,ios,swift,push-notification,watchkit,Ios,Swift,Push Notification,Watchkit,我想向apple watch通知添加值(当前屏幕使用硬编码数据): 我想为这些字段添加的值是:“金额”、“在”和“何时”。如何从PushNotificationPayload.apns文件中添加和获取此值并在通知中显示 这是PushNotificationPayload.apns文件: { "aps": { "alert": { "body": "New Transaction\n\n", "title": "Optional title" },

我想向apple watch通知添加值(当前屏幕使用硬编码数据):

我想为这些字段添加的值是:“金额”、“在”和“何时”。如何从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."
}
这些是步骤,

  • 创建一个新类,它是
    WKUserNotificationInterfaceController
    的子类

  • 从情节提要中,选择要通知的动态界面场景(如果尚未创建该场景,请在静态场景的属性检查器中启用“Has Dynamic Interface”),并在Identity检查器中按上述创建方式设置自定义类

  • 现在修改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);
    }
    
  • 最后是调试:

  • 在顶部选择目标,然后选择编辑方案

  • 单击底部的复制方案,并给出您的自定义名称,如“NOTIFICATION Mywatchkitapp”等

  • 然后,选择WatchKit Interface to Dynamic Notification,将通知有效负载发送到PushNotificationPayload.apns文件,并为此目标运行


Hi@DH14-S L,我想尝试一下您的解决方案。但是,目前我在运行自定义长外观通知时遇到问题。我在这里发布了一个错误:确保故事板和负载中的“category”键是正确的。然后检查您是否尚未在“willActivate”上编写任何长时间运行的代码。不幸的是,它仍然无法解决错误:(甚至创建一个新的项目并设置一个新的WITKIT C++目标,并启用了自定义通知,也会给出这样的错误……这是令人沮丧的……问题。我有很多测试APN的分类。但是我很好奇,如何从服务器传递一个真实世界的通知。(或其他)服务器将通知从服务器发送到实际设备的机制是什么?只需使用我们的开发证书的Apple服务器即可?我们如何通过TestFlight(或其他方法)测试真实世界的通知?我这样设置了我的.apns文件,但在watch kit通知接口中,方法didReceive(通知:),通知中不包含
金额
时和
时。有什么提示吗?