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

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

问题描述

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

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

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

这适用于ios和android。
但由于某种原因,我们需要发送 title body 声音表示数据有效负载中的密钥,需要删除通知有效负载。

This works for ios and android. But for some reason, 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 at iOS side.

是否通知键是显示的mandetory在iOS中 banner

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

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

How to use 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_available priority 具有各种组合的键。
通过所有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和网络

  • 向主题发送消息时

所有应用实例解释的公共密钥,无论平台如何是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和Web发送通知,而不是Android,您必须使用两个单独的键块,一个用于iOS,另一个用于Web。

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平台设置较长的生存时间,同时设置APN(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天全站免登陆