android - 如何向特定平台发送通知?

标签 android ios firebase notifications firebase-cloud-messaging

我想向 Android 和 iOS 发送两个不同的通知。我想向 iOS 发送一条通知消息,这样 iOS 会显示一条很好的通知。对于 Android,我想发送一条数据消息,这样我就可以在 Android 和后台处理通知(因为我没有在后台收到回调,想自己处理)。

我查看了文档,但找不到关于发送到特定平台的任何信息。我该怎么做?

也欢迎提出其他关于如何执行此操作的建议,但请记住,我特别想通过 Android 上的回调 (onMessageReceived) 自己处理通知

最佳答案

更新:最近为 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"
       }
     }
   }
 }

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.


我记得以前回答过类似的问题,但好像找不到了。目前没有选项可以指定将消息发送到哪个平台。执行此操作的最简单方法是使用主题消息传递。

每次第一次生成 token 时,您从客户端应用程序确定平台类型并将它们订阅到相应的主题(例如 topics/(Android/iOS)_<Your App Name>),然后根据需要发送消息。

跟踪来自您的服务器的注册 token 也很好,如果您使用的是 Firebase DB,您可以将它们放在一个节点中:

/pushTokens
  /android
    /{userId} : string
  /ios
    /{userid}: string

这将允许您从后端检查并在发送单个消息时根据需要调整您的负载。

关于android - 如何向特定平台发送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955979/

相关文章:

java - ObjectOutputStream 和 java.io.StreamCorruptedException

android - fatal error : Box2D. h:没有那个文件或目录

objective-c - Objective-C 中字符串与数组元素的串联

ios - 我如何自动化 Xamarin.iOS 单元测试项目

java - 创建按钮时出现我不明白的奇怪错误

android - 如何使用 retrofit2 创建 json 对象数组的查询

ios - 在字典/数组中高效存储和检索大量对象

firebase - 删除 LiveData 返回函数内的 Firestore 快照监听器

javascript - Firestore - 听特定的字段变化?

firebase - 如何在 Flutter 中手动分配 Listview.builder 的第一个元素?