消息推送支持集成 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
操作步骤如下:
-
在 Firebase 控制台添加应用。
登录 Firebase 控制台,参考 Firebase 文档 完成应用注册。
-
将 Firebase Android 配置文件添加到您的应用。
下载
google-services.json
配置文件,将配置文件放置到项目主 module 目录下。 -
在根目录
build.gradle
的buildScript
依赖中加入 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
// ...
}
}
-
在主 module 工程的
build.gradle
中应用 Google 服务插件。apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services' // Google Services plugin
android {
// ...
}
-
在主 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
操作步骤如下:
- 接入 MPS 依赖,mPaaS 基线版本不低于 10.1.68.19。
-
在主 module 工程的
build.gradle
中加入 FCM Adapter 依赖。dependencies {
implementation 'com.mpaas.push:fcm-adapter:0.0.1@aar'
}
-
在
AndroidManifest.xml
文件中加入MPaaSNcActivity
。如果已加入,请检查是否与下方配置一致。<activity
android:name="com.alipay.pushsdk.thirdparty.MPaaSNcActivity"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="com.alipay.pushsdk.thirdparty.MPaaSNcActivity" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
-
接收推送消息。
-
当应用在前台时,FCM 消息以透传消息方式传递给应用,您可以在自己实现的
AliPushRcvService
派生类的handleActionReceived
方法中收到消息对象,此时clicked
参数值为false
。 -
当应用在后台时,FCM 消息以通知栏消息展示,点击通知栏后
handleActionReceived
方法被调用,此时clicked
参数值为true
。
-
-
(可选)可通过注册消息接收器来获取 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();
}
}
}