Watch App 通知的负载

Payload for the Watch App Notification

在 Xcode 中创建新的 Apple Watch App 时,会创建以下 APNS 负载示例:

{
    "aps": {
        "alert": {
            "body": "Test message",
            "title": "Optional title"
        },
        "category": "myCategory"
    },

    "WatchKit Simulator Actions": [
        {
            "title": "First Button",
            "identifier": "firstButtonAction"
        }
    ],

    "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." }

我对在警报词典中使用 body 和标题感到困惑。以下负载通常用于 iOS 个应用程序:

{
    "aps": {
        "alert": "Test message",
        "title": "Opt title",
        "category": "default"
    },
    "WatchKit Simulator Actions": [
        {
            "title": "First Button",
            "identifier": "firstButtonAction"
        }
    ],

    "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."
}

哪个是正确的方法?尽管以这种方式创建了默认的负载文件,Apple documentation 提供了使用后一种变体的屏幕截图。

根据 Apple 文档,您可以在 aps 字典中找到以下键:alertbadge , soundcontent-available。这里是您可以在 alert 字典中拥有的键:title、body、title-loc-args 等。您可以在此处查看推送通知编程指南以获取更多信息: Push Notification Programming Guide

同时为您的 WatchKit ExtensionApple Watch Programming Guide

检查此项

另外,你描述的第二种方式应该是正确的方式。我刚刚创建了一个 Watch App,它包含示例负载。 category 放在 alert 外面的 aps 里面,在这种情况下应该是不正确的。希望这可以帮助。这意味着 aps 只能包含这四个默认属性。因此,category 应该包含在 alert.

Apple Documentation for Push Notification Payload 提到了一个注释,

Note: If you want the device to display the message text as-is in an alert that has both the Close and View buttons, then specify a string as the direct value of alert. Don’t specify a dictionary as the value of alert if the dictionary only has the body property.

所以根据上面的说明,两种格式都是正确的,但是当您只需要在 Alert 中使用 body 属性 时,我们应该使用第二种格式(直接使用文本值而不是字典进行警报)。如果您需要使用 Alert 的其他 child 属性,例如 body、title...,那么您应该使用第一种格式(Alert with dictionary as value)。

Local and Remote Notification Programming Guide(Table3-1)中,alert键的值类型可以是字符串或字典,如Dhawal 说,两种格式都是正确的。

如果alert是字典,它可以包含titlebodytitle-loc-key等(Table 3-2)。 title 键的用途是什么?这个key是在iOS 8.2中添加的,里面包含了WatchKit,WatchKit有一个Short-Look Notification接口,没有足够的space来进行full notification,所以Apple Watch使用title来描述短视通知中通知和显示的目的。


(来源:edgekey.net

在这张照片中,“格雷的生日”是alert中的title。因为在模拟器中看不到短视通知,所以你应该在 REAL Apple Watch 中测试 title 键的结果。