移动开发平台 mPaaS 接入 FCM 推送渠道

By | 2021年4月23日

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

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

前提条件

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

  • 采用原生 AAR 方式接入。FCM 仅支持以原生 AAR 方式接入,不支持 mPaaS Inside 和 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 服务插件。

        
    1. buildscript {
    2. repositories {
    3. // Check that you have the following line (if not, add it):
    4. google() // Google's Maven repository
    5. }
    6. dependencies {
    7. // ...
    8. // Add the following line:
    9. classpath 'com.google.gms:google-services:4.3.4' // Google Services plugin
    10. }
    11. }
    12. allprojects {
    13. // ...
    14. repositories {
    15. // Check that you have the following line (if not, add it):
    16. google() // Google's Maven repository
    17. // ...
    18. }
    19. }
  4. 在主 module 工程的 build.gradle 中应用 Google 服务插件。

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

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

接入 mPaaS

操作步骤如下:

  1. 接入 MPS 依赖,mPaaS 基线版本不低于 10.1.68.19。
  2. 在主 module 工程的 build.gradle 中加入 FCM Adapter 依赖。

        
    1. dependencies {
    2. implementation 'com.mpaas.push:fcm-adapter:0.0.1@aar'
    3. }
  3. AndroidManifest.xml 文件中加入 MPaaSNcActivity。如果已加入,请检查是否与下方配置一致。

        
    1. <activity
    2. android:name="com.alipay.pushsdk.thirdparty.MPaaSNcActivity"
    3. android:theme="@android:style/Theme.Translucent">
    4. <intent-filter>
    5. <action android:name="com.alipay.pushsdk.thirdparty.MPaaSNcActivity" />
    6. <action android:name="android.intent.action.VIEW" />
    7. <category android:name="android.intent.category.DEFAULT" />
    8. </intent-filter>
    9. </activity>
  4. 接收推送消息。

    • 当应用在前台时,FCM 消息以透传消息方式传递给应用,您可以在自己实现的 AliPushRcvService 派生类的 handleActionReceived 方法中收到消息对象,此时 clicked 参数值为 false

    • 当应用在后台时,FCM 消息以通知栏消息展示,点击通知栏后 handleActionReceived 方法被调用,此时 clicked 参数值为 true

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

    示例如下:

        
    1. <receiver android:name=".push.FcmErrorReceiver" android:exported="false">
    2. <intent-filter>
    3. <action android:name="action.mpaas.push.error.fcm.init" />
    4. </intent-filter>
    5. </receiver>
        
    1. package com.mpaas.demo.push;
    2. import android.content.BroadcastReceiver;
    3. import android.content.Context;
    4. import android.content.Intent;
    5. import android.widget.Toast;
    6. public class FcmErrorReceiver extends BroadcastReceiver {
    7. @Override
    8. public void onReceive(Context context, Intent intent) {
    9. String action = intent.getAction();
    10. if ("action.mpaas.push.error.fcm.init".equalsIgnoreCase(action)) {
    11. Toast.makeText(context, "fcm error " + intent.getIntExtra("error", 0), Toast.LENGTH_SHORT).show();
    12. }
    13. }
    14. }

请关注公众号获取更多资料

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注