如何向特定平台发送通知? [英] How to send a notification to specific platform?

查看:123
本文介绍了如何向特定平台发送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



还欢迎其他有关如何做到这一点的建议,但请记住,我特别希望通过Android上的回调来处理通知(onMessageReceived)

解决方案

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


跨平台自定义消息



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




  • 由接收消息的所有应用程序实例解释的一组公用密钥。

  • 特定于平台的密钥块,仅由在指定平台上运行的应用程序实例解释。



特定于平台的块使您可以灵活地为不同平台自定义消息,以确保在收到消息时正确处理它们。在许多情况下,在给定消息中使用公共密钥和特定于平台的密钥是有意义的。



何时使用公共密钥




  • 每当您在所有平台上定位应用实例时 - iOS,Android和网络

  • 您正在向主题发送消息



所有应用程序实例(无论平台如何)解释的公共密钥都是消息.notification.title message.notification.body message.data



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




  • 当您只想发送字段时特定平台

  • 除公共密钥外,还要发送特定于平台的字段



每当您只想将值发送到特定平台,不要使用公共密钥;使用特定于平台的密钥块。例如,要仅向iOS和Web发送通知,而不是Android,您必须使用两个单独的键块,一个用于iOS,另一个用于Web。



当您正在发送具有特定传递选项的消息,使用特定于平台的密钥来设置它们。如果需要,您可以为每个平台指定不同的值;但即使您希望跨平台设置基本相同的值,也必须使用特定于平台的密钥。这是因为每个平台可能会稍微不同地解释该值 - 例如,生存时间在Android上设置为到期时间(以秒为单位),而在iOS上则设置为到期日期。 / p>

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



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




  • 为Android和Web平台设置较长的生存时间,同时设置APN(iOS)消息优先级低设置

  • 设置相应的键以定义用户点击Android和iOS上的通知的结果 - click_action ,和类别。分别。




  {
message:{
token:bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...,
notification:{
title :比赛更新,
身体:加时赛阿森纳进球,得分现在是3-0
},
android:{
ttl :86400s,
通知{
click_action:OPEN_ACTIVITY_1
}
},
apns:{
标题:{
apns-priority:5,
},
payload:{
aps:{
category: NEW_MESSAGE_CATE GORY
}
}
},
webpush:{
headers:{
TTL:86400
}
}
}
}




请参阅 HTTP v1参考文档有关消息正文中特定于平台的块中可用的键的完整详细信息。有关构建包含邮件正文的发送请求的详细信息,请参阅构建发送请求







我记得之前回答过类似的问题,但似乎无法找到它。目前没有选项可以指定发送消息的平台。最简单的方法是使用主题消息。



每次首次生成令牌时,您可以从客户端应用程序确定平台类型并订阅它们到相应的主题(例如主题/(Android / iOS)_<您的应用名称> ),然后根据需要确定消息。



跟踪服务器的注册令牌也很好,如果你使用的是Firebase DB,你可以将它们放在一个节点内:

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

这可以让你从后端检查并在发送单个邮件时根据需要调整有效负载。


I want to send two different notifications to Android and iOS. I want to send a notification message to iOS, this way iOS displays a nice notification. For Android I want to send a data message, this way I can handle the notification on Android and also in the background (because I don't get a callback in the background and want to handle it myself).

I looked through the docs, but couldn't find anything about sending to a specific platform. How do I do that?

Also other suggestions on how to do this are welcome, but keep in mind that I specifically want to handle the notification myself through a callback on Android (onMessageReceived)

解决方案

Update: A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called 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.


I remember answering a similar question before but cant seem to find it. There is currently no option to specify which platform a message would be sent. The simplest way you could do this is by using Topics Messaging.

Everytime the token is generated for the first time, you determine from your client app the Platform type and subscribe them to the corresponding topic (e.g. topics/(Android/iOS)_<Your App Name>), then sned the messages as needed.

It's also good to keep track of the registration tokens from your Server, if you're using Firebase DB, you could put them inside a node:

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

This would let you check from your backend side and adjust your payload as needed when sending single messages.

这篇关于如何向特定平台发送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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