Skip to main content

Overview#

Besides Events, push notifications are another way to stay up to date with changes in your chat in real time.

To receive push notifications from Stream Chat, you'll need to:

  1. Configure your push notification provider on the Stream Dashboard.
  2. Add the client-side integration for the chosen provider in your app.

We support the following providers:

We ship an individual artifact for each of these to make client-side integration with their service quick and simple. See their individual documentation pages linked above for details. You need to add the list of PushDeviceGenerator you want to use:

val notificationConfig = NotificationConfig(    pushDeviceGenerators = listOf(        // PushDeviceGenerator    ),)
ChatClient.Builder("api-key", context)    .notifications(notificationConfig, notificationHandler)    .build()
note

Push notifications are disabled by default on the client side. You can enable it by setting the NotificationConfig::pushNotificationEnabled property to true. Keep in mind that the SDK creates a NotificationChannel during its initialization if push notifications are enabled client-side.

Customizing Push Notifications#

If you want, you can also customize how the push notifications work.

You can customize push notifications in the following ways:

  • Overriding resources: Lets you customize notification's icon and text resources
  • NotificationHandlerFactory: Uses the styles we provide but customizes an intent the user triggers when clicking on a notification.
  • NotificationHandler: Lets you fully customize how notifications are shown and dismissed
  • NotificationConfig::shouldShowNotificationOnPush: Lets you decide whether to show a notification after receiving a push

Overriding resources#

You can provide custom icon and text resources displayed in notifications by overriding them:

info

The SDK supports multiple languages. Make sure to override resources in all languages used in your app. Refer to Adding Localization guide for more details.

Using our NotificationHandlerFactory#

Our NotificationHandlerFactory provides a way to create a default implementation of NotificationHandler based on the Android API Level and lets you customize an intent that a user triggers by clicking the notification. This function lets you provide a custom Intent that can point to any Activity in your app. You can also add custom data to the Intent if you need it.

val notificationHandler = NotificationHandlerFactory.createNotificationHandler(    context = context,    newMessageIntent = {        messageId: String,        channelType: String,        channelId: String,         ->        // Return the intent you want to be triggered when the notification is clicked        val intent: Intent = [....]        intent    })
ChatClient.Builder("api-key", context)    .notifications(notificationConfig, notificationHandler)    .build()

Customize Notification Style#

The SDK lets you define the theming and behavior of the notification UI that users see after they receive a push notification. To do this, implement the NotificationHandler interface and show your own notification. If you want to dismiss notifications when a user visits a channel or logs off of the app, you will need to implement methods for dismissing notifications on the NotificationHandler class.

class MyNotificationHandler(private val context: Context) : NotificationHandler {    private val notificationManager: NotificationManager by lazy {        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager    }
    override fun showNotification(channel: Channel, message: Message) {        NotificationCompat.Builder(context, notificationChannelId)            ... // Configure your own notification            .build()        notificationManager.notify(notificationId, notification)    }
    override fun dismissChannelNotifications(channelType: String, channelId: String) {        // Dismiss all notification related with this channel    }
    override fun dismissAllNotifications() {        // Dismiss all notifications    }}

Finally, pass as the NotificationHandler implementation to the ChatClient.Builder when initializing the Stream Android SDK:

val notificationHandler = MyNotificationHandler(context)
ChatClient.Builder("api-key", context)    .notifications(notificationConfig, notificationHandler)    .build()

Dismissing notifications#

Our MessageListView UI component and MessageList Compose UI Component automatically dismiss notifications related to a Channel when the user opens it.

If you need to dismiss them manually (for example, if you are using a custom MessageListView component) you can call the ChatClient::dismissChannelNotifications method, providing the channelType and channelId from the Channel that you would like to dismiss notifications:

ChatClient.dismissChannelNotifications("messaging", "general")

Notifications are also automatically dismissed when the user logs out from the SDK.

Multi-Bundle#

In some cases you could be interested on configure different PushProvider by the same Provider (For example, 2 different Firebase project) with different configuration. To achieve it you need to setup Multi-Bundle configuration on the Dashboard giving a name to each provider. Then, when a new device is registered, you need to add the desired providerName.

Device(    token = "token-generated-by-provider",    pushProvider = yourPushProvider,    providerName = "providerName",)

If you are using any of the artifacts integration we support, you can configure it by an extra attribute on the constructor of the PushProviderGenerator class implementation.

Did you find this page helpful?