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 {
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
//Push message will be received here when push notification triggered for this device
}
@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.

Cloud-Messaging-API-key

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

Integrate Firebase Crashlytics#

Step 1: Add the Crashlytics In your project-level build.gradle.

dependencies {
// Check for v4.3.3 or higher
classpath 'com.google.gms:google-services:4.3.13'
classpath "com.google.firebase:firebase-crashlytics-gradle:2.9.1"
}
allprojects {
repositories {
// Add repository
google() // Google's Maven repository
}
}

Step 2: Add Crashlytics in your app-level build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.crashlytics'
dependencies {
// Add dependency
implementation 'com.google.firebase:firebase-crashlytics:18.2.12'
}

Note: After added Crashlytics to your application. In first time it will take more than 12 hours to affect the Firebase Crashlytics UI.

Refer Firebase Crashlytics Document