All Products
Search
Document Center

Mobile Platform as a Service:Integrate FCM push channel

Last Updated:Feb 24, 2023

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 integration mode. Portal & Bundle integration 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.

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

     apply plugin: 'com.android.application'
     // Add the following line:
     apply plugin: 'com.google.gms.google-services'  // Google Services plugin
    
     android {
       // ...
     }
  5. Add the FCM SDK dependency to the build.gradle file of the main module.

     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

Perform the following steps:

  1. Add the FCM Adapter dependency to the build.gradle file of the main module.

     dependencies {
         implementation 'com.mpaas.push:fcm-adapter:0.0.2'
     }
  2. Integrate the MPS SDK, with reference to the requirements on mPaaS baseline:

    • For com.mpaas.push:fcm-adapter:0.0.2, the baseline must be 10.1.68.34 or later version.

    • For com.mpaas.push:fcm-adapter:0.0.1, the baseline must be 10.1.68.19 or later version.

  3. Receive push messages.

    Due to the features of FCM SDK, the messages pushed through the FCM channel may not always be received by the client through the FCM channel, but may be received through the self-built channel. The specific rules are:

    • If the app is in frontend, the messages are passed through to the app by FCM, and the app will receive the message through the self-built channel.

    • If the app is in backend or the app is killed, the messages are sent through FCM channel, and are displayed on the notification bar.

  4. (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:

     <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();
             }
         }
     }