Firebase Integration

Integrate FireBase to the Application#

Step 1: Create Project in the firebase console using your app package name. Ex: com.testapp

Step 2: After the project creation download the google-service.json file which is automatically generated.

Step 3: Add the google-service.json file to your android Application with in the app folder.

Step 4: Add the firebase dependencies in the project level build.gradle file.

buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
classpath 'com.google.gms:google-services:4.3.13' // google-services plugin
}
}
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
}

Step 5: Apply plugin in app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation platform('com.google.firebase:firebase-bom:30.3.1')
}
Refer Firebase Document

Integrate Firebase Push notification#

Step 1: Add the FCM dependency to your app-level build.gradle file.

implementation 'com.google.firebase:firebase-messaging:23.0.6'

Step 2: Add below line in your app manifest file.

<service android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

Step 3: Create MyFirebaseMessagingService file in your application that extends FirebaseMessagingService service.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
For inbuild Text and Media notification
*/
FirebaseUtils firebaseUtils = new FirebaseUtils()
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
//Push message will be received here when push notification triggered for this device
/**
Use below code for in-build text and media related notification that MirrorFly UI KIT SDK will take care.
*/
if (remoteMessage.getData().size() > 0) {
firebaseUtils.handleReceivedMessage(this, notificationData,R.drawable.chat) // R.drawable.chat Custom chat icon
}
}
@Override
public void onNewToken(@NonNull String token) {
//Whenever Device token will get updated/changed this method will be triggered
PushNotificationManager.updateFcmToken(token, isSuccess, message) -> {
});
}
}

Step 4: Enable Cloud messaging in FireBase, you can enable cloud messaging in the below Link. Enable cloud messaging

Step 5: Copy Cloud Messaging API key and configure it in the chat server to receive message and calls from Chat SDK.

You can find Cloud Messaging API key in Project Settings under Cloud Messaging tab. Please refer below screenshot.

Step 6: Open the AndroidManifest.xml and add below permissions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Step 7: Get the device token and add it in cloud message console.

FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
//Fetching FCM registration token failed
return;
}
//Fetching FCM registration token succeed
// Get new FCM registration token
String token = task.getResult();
PushNotificationManager.updateFcmToken(token, isSuccess, message) -> {
});
}
});