Android & Firebase

Last Edit: Sep 25 2020

Push Notifications for Android and Web

If you're looking to speed up your Android app development, you can use our Android Chat SDK to get started. It's feature rich and will speed up your development!

Using Firebase, your users apps can receive push notifications directly to their client app for new messages when offline. In order to push notifications to Android devices, you need to have an application on Firebase and configure your Stream account using the Firebase server key.

Retrieving the Server Key from Firebase

Step 1

From the Firebase Console, select the project your app belongs to

Step 2

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

Step 3

Navigate to the Cloud Messaging tab

Step 4

Under Project Credentials, locate the Server key and copy it

Step 5

Upload the Server Key in your chat dashboard

Step 6

Save your push notification settings changes

OR -

Upload the Server Key via API call


await client.updateAppSettings({
    firebase_config: {
        server_key: 'server_key',
        notification_template: `{"message":{"notification":{"title":"New messages","body":"You have {{ unread_count }} new message(s) from {{ sender.name }}"},"android":{"ttl":"86400s","notification":{"click_action":"OPEN_ACTIVITY_1"}}}}`,
        data_template: `{"sender":"{{ sender.id }}","channel":{"type": "{{ channel.type }}","id":"{{ channel.id }}"},"message":"{{ message.id }}"}`
    },
});
                    
Remeber to add google-services.json file to your project source directory. You can download it in the Firebase console. For more information take a look at Firebase setup tutorial https://firebase.google.com/docs/android/setup.

Registering a device at Stream Backend

Once you configure Firebase server key and set it up on Stream dashboard a device which is supposed to receive push notifications needs to be registered at Stream backend. This is usually done by requesting a Firebase device token and passing it to the backend as follows:


FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener {
    if (it.isSuccessful) {
        client.addDevice(it.result!!.token).enqueue { result ->
            if (result.isSuccess) {
                // device was successfully registered
            }  
        }
    }
}
                    
Device registration logic above is also performed automatically when you set up User data in the Chat or ChatClient objects. If you use Android Chat SDK or Android ChatClient low level SDK there is no need to register the device manually.

Setting up notification data payload at Stream Dashboard

1. Setup the following push notification data payload at Stream Dashboard:


{
  "message_id": "{{ message.id }}",
  "channel_id": "{{ channel.id }}",
  "channel_type": "{{ channel.type }}"
}
                    

2. If you are using Stream Chat Android SDK add the following notifications config into your Chat building logic:


Chat.Builder(appConfig.apiKey, this).apply {
            offlineEnabled = true
            notificationsConfig =
                NotificationConfig(
                    firebaseMessageIdKey = "message_id",
                    firebaseChannelIdKey = "channel_id",
                    firebaseChannelTypeKey = "channel_type"
                )
        }.build()
                    

If you are using only the low-level SDK (Stream Chat Android Client) then you need to use ChatClient API to configure notifications support:


ChatClient.Builder(apiKey, this)
  .notifications(NotificationsHandler(context, notificationsConfig))
  .build()
                    

3. Add FirebaseService to the AndroidManifest.xml, inside the element:


<service
    android:name="io.getstream.chat.android.livedata.service.sync.OfflineSyncFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
                    

If you are using low-level SDK (Stream Chat Android Client) then declare a different class in the manifest instead:


<service
    android:name="io.getstream.chat.android.livedata.service.sync.OfflineSyncFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
 </service>
                    

Possible issues

if RemoteMessage does not arrive (is not intercepted) check if notification template is defined correctly at the Stream Dashboard. Read more about difference between notification and data here.