Skip to main content

Firebase Cloud Messaging#

This page shows you how to receive Stream Chat push notifications using Firebase Cloud Messaging.

Configuring Notifications on the Stream Dashboard#

To be able to receive notifications from Stream, you need to provide your Firebase credentials to Stream.

Go to the Firebase Console, and select the project your app belongs to.

info

If you don't have a Firebase project yet, you'll have to create a new one.

Click on the gear icon next to Project Overview and navigate to Project settings:

Opening Firebase's Project settings

Navigate to the Service Accounts tab. Under Firebase Admin SDK section, click on Generate new private key button that will generate a json file with the Firebase Credentials.

Generate your Firebase Credentials json file

Open the Stream Dashboard. Navigate to the Chat Overview page for your app.

Navigating to the Chat Overview page on the Stream Dashboard

Scroll down and enable the Firebase switch. Paste the content of the json file with the Firebase Credentials that you downloaded during the previous step, and click Save to confirm your changes.

Setting up your Firebase Credentials on the Stream Dashboard

That's the server-side setup done. You can now receive push notifications from Stream Chat on the client side. To register devices and process these notifications, follow the steps below.

Receiving Notifications in the Client#

We provide an artifact with all the implementation needed to work with Firebase. To use it follow the next steps:

Start by adding Firebase to your Android project. You only need to set up the FCM dependencies and add a google-services.json file to your project source directory.

Next, add the Stream Firebase push provider artifact to your app's build.gradle file:

dependencies {    implementation "io.getstream:stream-chat-android-pushprovider-firebase:$stream_version"}

Finally, add the FirebasePushDeviceGenerator to your NotificationConfig and pass it into the ChatClient.Builder when you initialize the SDK:

val notificationConfig = NotificationConfig(    pushDeviceGenerators = listOf(FirebasePushDeviceGenerator()))ChatClient.Builder("apiKey", context)    .notifications(notificationConfig)    .build()
caution

Make sure that ChatClient is always initialized before handling push notifications. We highly recommend initializing it in the Application class.

That's it! You can now receive push notifications from Stream via Firebase!

Using a Custom Firebase Messaging Service#

The Stream Firebase push provider artifact includes an implementation of FirebaseMessagingService that will send new Firebase tokens and incoming push messages to the Stream SDK.

If you're also using Firebase notifications for other things in your app, you can use your own custom service instead. This should make the following calls to the FirebaseMessagingDelegate class:

class CustomFirebaseMessagingService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {        // Update device's token on Stream backend        try {            FirebaseMessagingDelegate.registerFirebaseToken(token)        } catch (exception: IllegalStateException) {            // ChatClient was not initialized        }    }
    override fun onMessageReceived(message: RemoteMessage) {        try {            if (FirebaseMessagingDelegate.handleRemoteMessage(message)) {                // RemoteMessage was from Stream and it is already processed            } else {                // RemoteMessage wasn't sent from Stream and it needs to be handled by you            }        } catch (exception: IllegalStateException) {            // ChatClient was not initialized        }    }}
note

Make sure that your custom service has an <intent-filter> priority higher than -1 to override our default service. (This priority is 0 by default.)

Did you find this page helpful?