日常bb

日常bb

Firebase Cloud Messaging(FCM推送)

132
2024-07-05
Firebase Cloud Messaging(FCM推送)

密钥文件

您的 Firebase 项目

进入项目 > 设置 > 项目设置 > 服务账号 > 生产新的私钥。

Firebase API Reference

服务器发送

服务器环境向 FCM 后端发送消息

推荐两种方式:

  1. 使用 Firebase Admin SDK。
  2. 使用 FCM HTTP v1 API。

数据格式

FCM 消息简介

{
  "message": {
    "topic": "news",
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification": {
      "title": "Breaking News",
      "body": "New news story available."
    },
    "data": {
      "story_id": "story_12345"
    },
    "android": {
      "notification": {
        "click_action": "TOP_STORY_ACTIVITY",
        "body": "Check out the Top Story"
      }
    },
    "apns": {
      "payload": {
        "aps": {
          "category" : "NEW_MESSAGE_CATEGORY"
        }
      }
    }
  }
}

数据消息:无 notification 字段。
通知消息:有 notification 字段。

消息类型

跨平台自定义消息

跨平台自定义消息-何时使用通用字段

Android端实现

在 Android 应用中接收消息

官方文档是扩展com.google.firebase.messaging.FirebaseMessagingService#onMessageReceived重写onMessageReceived去处理。

或者使用自主性更高的方式,扩展com.google.firebase.messaging.FirebaseMessagingService#onMessageReceived,但重写com.google.firebase.messaging.EnhancedIntentService#handleIntenthandleIntent方法去处理。