文档

FCM 推送

更新时间:

消息推送支持集成 Firebase 云信息传递(Firebase Cloud Messaging,简称 FCM)通道,以满足 App 在海外安卓设备上的使用需求。

下面介绍 FCM 推送通道的接入过程。

前提条件

接入 FCM 前,需要满足以下几个条件:

  • 采用原生 AAR 方式接入。FCM 仅支持以原生 AAR 方式接入,不支持 Portal & Bundle 接入。

  • Gradle 版本大于 4.1。

  • 使用 AndroidX。

  • com.android.tools.build:gradle 为 3.2.1 或以上版本。

  • compileSdkVersion 为 28 或以上版本。

接入 FCM SDK

操作步骤如下:

  1. 在 Firebase 控制台添加应用。

    登录 Firebase 控制台,参考 Firebase 文档 完成应用注册。

  2. 将 Firebase Android 配置文件添加到您的应用。

    下载 google-services.json 配置文件,将配置文件放置到项目主 module 目录下。

  3. 在根目录build.gradlebuildScript 依赖中加入 Google 服务插件。

     buildscript {
    
       repositories {
         // Check that you have the following line (if not, add it):
         google()  // Google's Maven repository
       }
    
       dependencies {
         // ...
    
         // Add the following line:
         classpath 'com.google.gms:google-services:4.3.4'  // Google Services plugin
       }
     }
    
     allprojects {
       // ...
    
       repositories {
         // Check that you have the following line (if not, add it):
         google()  // Google's Maven repository
         // ...
       }
     }
  4. 在主 module 工程的 build.gradle 中应用 Google 服务插件。

     apply plugin: 'com.android.application'
     // Add the following line:
     apply plugin: 'com.google.gms.google-services'  // Google Services plugin
    
     android {
       // ...
     }
  5. 在主 module 工程的 build.gradle 中加入 FCM SDK 依赖。

     dependencies {
         // Import the BoM for the Firebase platform
         implementation platform('com.google.firebase:firebase-bom:26.1.1')
    
         // Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
         // When using the BoM, you don't specify versions in Firebase library dependencies
         implementation 'com.google.firebase:firebase-messaging'
         implementation 'com.google.firebase:firebase-analytics'
     }

接入 mPaaS

操作步骤如下:

  1. 在主 module 工程的 build.gradle 中加入 FCM Adapter 依赖。

     dependencies {
         implementation 'com.mpaas.push:fcm-adapter:0.0.2'
     }
  2. 接入消息推送组件,mPaaS 基线版本要求如下:

    • 对于 com.mpaas.push:fcm-adapter:0.0.2, 基线版本不能低于 10.1.68.34。

    • 对于 com.mpaas.push:fcm-adapter:0.0.1, 基线版本不能低于 10.1.68.19。

  3. 接收推送消息。由于 FCM SDK 本身的特性,通过控制台或服务端向 FCM 通道推送的消息,客户端并不总是会从 FCM 通道接收到,也可能会从自建通道接收到。具体规则为:

    • 当应用在后台或应用进程被杀死时,消息会从 FCM 通道收到,直接展示在通知栏,和其他厂商通道一样。

    • 当应用在前台时,消息会被 FCM 透传给应用,应用会从自建通道收到消息。

  4. (可选)可通过注册消息接收器来获取 FCM 初始化失败的错误信息,具体参见 错误码说明文档。 示例如下:

             <receiver android:name=".push.FcmErrorReceiver" android:exported="false">
                 <intent-filter>
                     <action android:name="action.mpaas.push.error.fcm.init" />
                 </intent-filter>
             </receiver>
     package com.mpaas.demo.push;
    
     import android.content.BroadcastReceiver;
     import android.content.Context;
     import android.content.Intent;
     import android.widget.Toast;
    
     public class FcmErrorReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
             if ("action.mpaas.push.error.fcm.init".equalsIgnoreCase(action)) {
                 Toast.makeText(context, "fcm error " + intent.getIntExtra("error", 0), Toast.LENGTH_SHORT).show();
             }
         }
     }
  • 本页导读 (0)
文档反馈