All Products
Search
Document Center

Access FCM push channel

Last Updated: Feb 05, 2021

MPS supports integrating the Firebase Cloud Messaging (FCM) push channel to satisfy the message push requirements on overseas Android devices.

The following sections describe how to integrate the FCM push channel.

Prerequisites

Before you integrate FCM, ensure that the following conditions are met:

  • Adopt native AAR access mode. mPaaS Inside and Portal & Bundle access modes don’t work for FCM.
  • Gradle must be 4.1 or later versions.
  • AndroidX is used.
  • com.android.tools.build:gradle must be 3.2.1 or a later version.
  • compileSdkVersion must be 28 or a later version.

Integrate FCM SDK

Perform the following steps:

  1. Add your App in the Firebase console.

    Log on to the Firebase console and register your App. See Firebase documentation.

  2. Add the Firebase Android configuration file to your App.

    Download the configuration file google-services.json and move the file to the main module of your project.

  3. Add the Google service plug-in to the buildScript dependency in the root-level build.gradle file.

    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. Apply the Google service plug-in in the build.gradle file of the main module.

    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. Add the FCM SDK dependency to the build.gradle file of the main module.

    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. }

Access mPaaS

Perform the following steps:

  1. Add the MPS dependency. mPaaS baseline must be 10.1.68.19 or a later version.
  2. Add the FCM Adapter dependency to the build.gradle file of the main module.

    1. dependencies {
    2. implementation 'com.mpaas.push:fcm-adapter:0.0.1@aar'
    3. }
  3. Add MPaaSNcActivity to the AndroidManifest.xml file. If the code has already been added, check whether the MPaaSNcActivity is consistent with the following configuration.

    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. Receive push messages.

    • If the App is in the foreground, FCM messages are passed through to the App. You can receive the message objects in the handleActionReceived method of the derived class AliPushRcvService. The value of clicked is false.
    • If the App is in the background, FCM messages are displayed on the notification bar. After you click the notification bar, the handleActionReceived method is called and the value of clicked is true.
  5. (Optional) You can register a message receiver to obtain an error message when the FCM initialization fails. For details, see Error codes.

    Refer to the following sample code:

    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. }