gpt4 book ai didi

android - 适用于 iOS 和 Android 的 FCM 远程通知负载

转载 作者:IT老高 更新时间:2023-10-28 23:24:59 26 4
gpt4 key购买 nike

我们正在使用 FCM 为 iOS 和 Android 发送远程通知。以下是我们从后端发送的负载。

options = {
notification: {
title: "title",
body: body,
sound: 'default'
},
priority: "high",
content_available: true,
data: {
type: 'type',
id: id,
}
}

这适用于 ios 和 android。但是由于某种原因,android端我们需要为data payload中的keys发送titlebodysound并且需要移除 notification 有效载荷。

现在,当应用程序处于非 Activity 状态时,通知不会在 ios 端接收,横幅通知未到达,但在应用程序处于 Activity 状态时正在接收数据。我们需要 iOS 端的横幅。

notification 键是强制在 iOS 中显示 banner 的吗?

如何为 iOS 和 Android 使用相同的负载。

options = {

priority: "high",
content_available: true,
data: {
title: "title",
body: body,
sound: 'default'
type: 'type',
id: id,
}
}

还尝试使用各种组合添加 content_availablepriority 键。浏览了所有 FCM 文档,但仍然令人困惑。帮助/建议表示赞赏。

最佳答案

最近为 FCM 添加了一项功能,该功能提供了为特定平台提供特定参数的选项,称为 Platform Overrides :

Customizing a message across platforms

Messages sent by the FCM v1 HTTP protocol can contain two types of JSON key pairs:

  • a common set of keys to be interpreted by all app instances that receive the message.
  • platform-specific blocks of keys interpreted only by app instances running on the specified platform.
  • Platform-specific blocks give you flexibility to customize messages for different platforms to ensure that they are handled correctly when received. In many scenarios, it makes sense to use both common keys and platform-specific keys in a given message.

When to use common keys

  • Whenever you're targeting app instances on all platforms — iOS, Android, and web
  • When you are sending messages to topics

The common keys that are interpreted by all app instances regardless of platform are message.notification.title, message.notification.body, and message.data.

When to use platform-specific keys

  • When you want to send fields only to particular platforms
  • To send platform-specific fields in addition to the common keys

Whenever you want to send values to specific platforms only, don't use common keys; use platform-specific key blocks. For example, to send a notification to only iOS and web but not Android, you must use two separate blocks of keys, one for iOS and one for web.

When you are sending messages with specific delivery options, use platform-specific keys to set them. You can specify different values per platform if you want; but even when you want to set essentially the same value across platforms, you must use platform-specific keys. This is because each platform may interpret the value slightly differently — for example, time-to-live is set on Android as an expiration time in seconds, while on iOS it is set as an expiration date.

Example: notification message with platform-specific delivery options

The following v1 send request sends a common notification title and content to all platforms, but also sends some platform-specific overrides. Specifically, the request:

  • sets a long time-to-live for Android and Web platforms, while setting the APNs (iOS) message priority to a low setting
  • sets the appropriate keys to define the result of a user tap on the notification on Android and iOS — click_action, and category, respectively.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Match update",
"body":"Arsenal goal in added time, score is now 3-0"
},
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "5",
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush":{
"headers":{
"TTL":"86400"
}
}
}
}

HTTP v1 reference documentation有关消息正文中特定于平台的 block 中可用 key 的完整详细信息。有关构建包含消息正文的发送请求的更多信息,请参阅 Build Send Requests .

关于android - 适用于 iOS 和 Android 的 FCM 远程通知负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41562823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com