Integrate FCM (for Android)
This article is applicable for integrating Google FCM on Android devices.
Google FCM (Firebase Cloud Messaging) is a cross-platform messaging solution that lets you reliably deliver messages.
Before using ZIM SDK's offline push notification feature, please implement the Google FCM push notification first by referring to this document.
After Google FCM push notification is enabled, the ZPNs SDK used the Google notification channel directly. The notification channels of other push service providers won't be available.
Prerequisites
Before the push notification implementation using FCM, make sure you complete the following:
-
Your mobile device supports the GMS (Google Mobile Services), and has connected to the network.
-
Integrate the latest version of ZIM SDK. For details, see Getting Started - Integrate the SDK.
-
Integrate the ZPNs SDK. For more, refer to the Implement offline push notification.
-
Complete the Firebase-related steps below:
-
Go to Firebase, log in with your Google account.
-
Select Add project, and complete the project information.
-
Click on the area shown below.
-
Click the button shown below to create an Android application.
-
Enter your Application's package name, and click Register App.
-
Download a
google-services.json
file. And move the downloadedgoogle-services.json
file into your Android app module root directory when integrating the SDK.
-
Implementation steps
Add the FCM push notification to the Android app
-
In the root level
build.gradle
file, configure Google's Maven repository for adding Google dependencies.<project>/build.gradlebuildscript { repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository } dependencies { ... // Add this line classpath 'com.google.gms:google-services:4.3.10' } } allprojects { ... repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository ... } }
1 -
In the module level
build.gradle
file, add the Firebase-related dependencies.<project>/<app module>/build.gradleapply plugin: 'com.android.application' // Add this line apply plugin: 'com.google.gms.google-services' dependencies { // Import the Firebase BoM implementation platform('com.google.firebase:firebase-bom:29.3.1') // Add the dependency for the Firebase SDK for Google Analytics // When using the BoM, don't specify versions in Firebase dependencies implementation 'com.google.firebase:firebase-analytics' // Add the dependencies for any other desired Firebase products // https://firebase.google.com/docs/android/setup#available-libraries }
1 -
In the module level
build.gradle
file (path: <project>/<app module>/build.gradle), adding the Google notification dependencies.Untitledimplementation 'com.google.firebase:firebase-messaging:21.1.0'
1
- After configuring the above, call the
setPushConfig
method with"enableFCMPush": true
to set up a Google notification channel.UntitledZPNs.setPushConfig({"enableFCMPush": true, "enableVivoPush": true});
1
-
Preventing code obfuscation
Add a
-keep
rule in the "proguard-rules.pro" file to protect the public classes of the FCM SDK from being obfuscated, ensuring the normal operation of FCM features.Untitled-dontwarn com.google.** -keep class com.google.** {*;}
1
In this case, you have added the FCM push notification to your Android app project.
Now, all you have left to do is go to the ZEGOCLOUD Admin Console to authorize the FCM. Check the following:
Add FCM credentials on Admin Console
-
Get a Firebase service account credential
The android project supports a Google service account, so you will need to get credential files using your Google service account, and allow ZEGOCLOUD Server to call the Firebase server API.
Suggested steps:
Go to Firebase Project Settings > Service accounts.
Click the Generate new private key, then confirm by clicking Generate Key. -
Upload FCM credentials
Log in to the ZEGOCLOUD Admin Console, select Service Management > In-app Chat tab, and add FCM credentials in the Notification configuration session.
At this point, you have completed the configuration of the FCM offline push notification.