适用于 iOS 和 Android 的 FCM 远程通知负载 [英] FCM remote notifications payload for iOS and Android

查看:19
本文介绍了适用于 iOS 和 Android 的 FCM 远程通知负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are using FCM to send remote notifications for both iOS and Android. The following are the payloads we are sending from the backend.

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

这适用于 ios 和 android.但是出于某种原因,android端我们需要为data有效载荷中的键发送titlebodysound并且需要删除 notification 有效负载.

This works for ios and android. But for some reason, the android side we need to send title,body and sound for keys in data payload and need to remove notification payload.

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

Now the notifications are not receiving ios side when app is not active, banner notifications are not arriving but the data is receiving when app is active. We need banners on the iOS side.

notification 键是在 iOS 中显示 banner 所必需的吗?

Is that notification key is mandatory to display banner in iOS?

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

How to use the same payload for both iOS and Android.

options = {

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

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

Also tried adding content_available and priority keys with various combinations. Gone through all FCM docs and it still confuses. Help/Suggestions appreciated.

推荐答案

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

A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides:

跨平台自定义消息

FCM v1 HTTP 协议发送的消息可以包含两种类型的 JSON 密钥对:

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

  • 一组通用的键,由接收消息的所有应用实例解释.
  • 特定于平台的密钥块仅由在指定平台上运行的应用实例解释.
  • 特定于平台的块让您可以灵活地为不同平台自定义消息,以确保在收到消息时得到正确处理.在许多情况下,在给定消息中同时使用通用密钥和特定于平台的密钥是有意义的.

何时使用通用键

  • 每当您将应用实例定位到所有平台(iOS、Android 和 Web)时
  • 当您向主题发送消息时

无论平台如何,所有应用实例都解释的通用键是 message.notification.title、message.notification.body 和 message.data.

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

何时使用特定于平台的密钥

  • 当您只想将字段发送到特定平台时
  • 除了通用密钥之外,还要发送特定于平台的字段

当您只想将值发送到特定平台时,请不要使用通用键;使用特定于平台的关键块.例如,要仅向 iOS 和网络而不是 Android 发送通知,您必须使用两个单独的密钥块,一个用于 iOS,一个用于网络.

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.

当您发送具有特定递送选项的消息时,请使用特定于平台的密钥进行设置.如果需要,您可以为每个平台指定不同的值;但即使您想跨平台设置基本相同的值,您也必须使用特定于平台的键.这是因为每个平台对值的解释可能略有不同 - 例如,生存时间在 Android 上设置为以秒为单位的到期时间,而在 iOS 上则设置为到期日期.

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.

示例:具有特定于平台的传递选项的通知消息

以下 v1 发送请求向所有平台发送通用通知标题和内容,但也发送一些特定于平台的覆盖.具体来说,请求:

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:

  • 为 Android 和 Web 平台设置较长的生存时间,同时将 APNs (iOS) 消息优先级设置为较低的设置
  • 设置适当的键以定义用户在 Android 和 iOS 上点击通知的结果 - 分别是 click_action 和 category.

{
  "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 参考文档,了解完整信息有关消息正文中特定于平台的块中可用密钥的详细信息.有关构建包含消息正文的发送请求的更多信息,请参阅构建发送请求.

See the HTTP v1 reference documentation for complete detail on the keys available in platform-specific blocks in the message body. For more information about building send requests that contain the message body, see Build Send Requests.

这篇关于适用于 iOS 和 Android 的 FCM 远程通知负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆