iOS 通知负载中是否需要 aps 字典?我们可以为 iOS 和 Android 使用相同的负载吗?

Is aps dictionary necessary in iOS notification payload? Can we have same payload for iOS and Android?

我刚开始研究 iOS 上的通知,看来 apple 已经定义了有效负载接收通知的格式。

所以目前,我正在使用这个负载,一切都按预期工作。我正在获取标题、副标题、body 声音、图像。

{"aps" : {
        "alert" : {
            "title" : "Introduction To Notification",
            "subtitle" : "Session 707",
            "body" : "New Notification Look Amazing"
        },
       "sound" : "default",
        "category" : "message",
        "badge" : 1,
        "mutable-content": 1
    },
    "attachment-url": "https://pusher.com/static_logos/320x320.png"
}

假设我想为 Android 和 iOS 使用一个有效负载。

在Android中是否定义了通知的标准格式,或者您可以在Android中设置任何数据并且客户端必须手动处理和显示这些通知?

如何创建适用于两者的负载?

cross-platform 有效载荷的更新:最近为 FCM 添加了一项功能,它提供了为特定平台提供特定参数的选项,称为 Platform Overrides


您发布的示例 payload 似乎是 in-line 和 official parameters for APNs. When using GCM or FCM,要使用的参数不同(请参阅链接)。

Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?

这取决于您打算使用哪种类型的消息负载。 GCM/FCM、notificationdata2 types of Messages

notification messages only have predefined set of parameters available, while data messages can be used to have custom key-value pairs. Both are usually handled by the client, but note that the behavior for Android and iOS 因您使用的消息类型而异(请参阅链接)。

How can I create a payload which works for both?

正如我在 other post 的评论部分中提到的那样:

You'll have to do the mapping in your own database/app server. Yes. What I was thinking here was every time a registration token is generated on the client app side, you send it to your database/app server along the type of device (i.e. "Android", "iOS"). So that when you'll be sending messages, you'll first have to check the type of device. I did say it's more work, but it's a sure way to give you control over things. AFAIK, it is the developer's responsibility to keep track of the registration tokens and any details that should be associated with it.

您不能将自定义标签放入 aps 标签中。以下是文档对它的描述:

提供商可以在 Apple 保留的 aps 命名空间之外指定自定义负载值。自定义值必须使用 JSON 结构化和原始类型:字典(对象)、数组、字符串、数字和布尔值。 所以在你的情况下你应该做这样的事情:

{
    "aps": {
        "alert": "Hello World",
        "sound": "default"
    },
    "Person": {
        "Address": "Your address",
        "Name": "Your Name",
        "Number": "XXXXXXXXXX"
    }
}

因此,您可以通过在主 JSON 中而不是在 "aps":

中查找它的键来读取您的自定义负载