Message Push Service supports the Firebase Cloud Messaging (FCM) channel for Android applications on devices outside China.
This topic describes how to integrate the FCM push channel.
Prerequisites
Before you integrate FCM, ensure that the following prerequisites are met:
You must integrate using the native AAR method. FCM only supports this method and does not support Portal & Bundle integration.
The Gradle version must be 4.1 or later.
Your project must use AndroidX.
The version of
com.android.tools.build:gradlemust be 3.2.1 or later.The
compileSdkVersionmust be 28 or later.
Integrate the FCM SDK
Follow these steps:
Add an application in the Firebase console.
Log in to the Firebase console and register your application. For more information, see the Firebase documentation.
Add the Firebase Android configuration file to your application.
Download the
google-services.jsonconfiguration file. Place the file in the app module directory of your project.Add the Google Services plugin to the
buildScriptdependencies in the root-levelbuild.gradlefile of your project.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 // ... } }Apply the Google Services plugin in your app module's
build.gradlefile.apply plugin: 'com.android.application' // Add the following line: apply plugin: 'com.google.gms.google-services' // Google Services plugin android { // ... }Add the FCM SDK dependency to your app module's
build.gradlefile.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' }
Integrate mPaaS
Follow these steps:
Add the FCM Adapter dependency to your app module's
build.gradlefile.dependencies { implementation 'com.mpaas.push:fcm-adapter:0.0.2' }Integrate the MPS component. The mPaaS baseline version must meet the following requirements:
For
com.mpaas.push:fcm-adapter:0.0.2, the baseline version must be 10.1.68.34 or later.For
com.mpaas.push:fcm-adapter:0.0.1, the baseline version must be 10.1.68.19 or later.
Receive push messages. Because of the behavior of the FCM SDK, the client does not always receive messages from the FCM channel when they are pushed from the console or the server-side. The client may receive them from the self-built channel instead. The delivery rules are as follows:
When the application is in the background or the application process is killed, messages are delivered through the FCM channel. These messages are displayed in the notification bar, similar to messages from other vendor channels.
When the application is in the foreground, FCM passes the messages to the application, which then receives them through the self-built channel.
(Optional) Register a message receiver to receive error messages if FCM initialization fails. For more information, see the error code documentation. The following code provides an example:
<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(); } } }