【问题标题】:Payload for the Watch App NotificationWatch 应用通知的有效负载
【发布时间】:2015-05-23 16:58:59
【问题描述】:

在 Xcode 中创建新的 Apple Watch 应用时,会创建以下 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." }

我对警报词典中正文和标题的使用感到困惑。 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 提供了使用后一种变体的屏幕截图。

【问题讨论】:

  • 我可以确认第一个变体确实有效。这就是我们用于有效载荷的形式。我们的应用已在商店中上线。
  • 是的,但我可以确认第二个也可以。

标签: ios apple-push-notifications watchkit


【解决方案1】:

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

如果alert是字典,它可以包含titlebodytitle-loc-key等(表3-2)。 title 密钥的用途是什么?这个键是在 iOS 8.2 中加入 WatchKit 的,WatchKit 有一个 Short-Look Notification 界面,没有足够的空间来完整通知,所以 Apple Watch 使用title 来描述通知的目的并在 Short-Look 中显示通知。


(来源:edgekey.net

在这张图片中,“格雷的生日”是alert 中的title。因为在模拟器中看不到 Short-Look Notification,所以应该在 REAL Apple Watch 中测试title 键的结果。

【讨论】:

  • 很棒的提示,我使用我的真实 Apple Watch 对其进行了测试,底部变体不起作用,只有使用警报字典和标题键的上部变体。 Apple 文档中的屏幕截图出现错误。
【解决方案2】:

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

请检查您的 WatchKit 扩展Apple Watch Programming Guide

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

【讨论】:

  • 该文档似乎已过时,因为也未提及类别键。
  • 实际上并非如此。从Apple Watch 编程指南,它也建议你去推送通知编程指南 作为参考。
  • 是的,但是第二种方式也应该是正确的;查看我提供的文档链接中的示例屏幕截图..
  • 根据您的链接,使用 alert 作为字符串和 alert 作为字典都是正确的。问题是必须在哪里放置新的“title”键;在警报字典内还是在同一级别?
  • 我现在已经确认第二个变体按预期工作,但它没有解释我在开篇文章中提到的 Apple 文档的另一页。除了文档不完整,也没有提到分类键。
【解决方案3】:

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

注意:如果您希望设备按原样显示消息文本 具有关闭和查看按钮的警报,然后指定一个字符串 作为 alert 的直接值。不要将字典指定为值 如果字典只有 body 属性,则警告。

因此,如上所述,两种格式都是正确的,但是当您只需要 Alert 中的 body 属性时,我们应该使用第二种格式(直接使用文本值而不是字典的警报)。如果您需要使用 Alert 的其他子属性,例如 body、title、...,那么您应该使用第一种格式(以字典为值的 Alert)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    相关资源
    最近更新 更多