val notificationConfig = NotificationConfig(
pushDeviceGenerators = listOf(
// PushDeviceGenerator
),
)
ChatClient.Builder("api-key", context)
.notifications(notificationConfig, notificationHandler)
.build()
Setup
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:
boolean pushNotificationEnabled = true;
List<PushDeviceGenerator> pushDeviceGeneratorList = new ArrayList<>();
NotificationConfig notificationConfig = new NotificationConfig(pushNotificationEnabled, pushDeviceGeneratorList);
new ChatClient.Builder("api-key", context)
.notifications(notificationConfig, notificationHandler)
.build();
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 dismissedNotificationConfig::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:
- We are using stream_ic_notification.xml drawable as the default notification’s icon
- Text resources are placed inside string.xml file
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()
boolean pushNotificationEnabled = true;
List<PushDeviceGenerator> pushDeviceGeneratorList = new ArrayList<>();
NotificationConfig notificationConfig = new NotificationConfig(pushNotificationEnabled, pushDeviceGeneratorList);
NotificationHandler notificationHandler = NotificationHandlerFactory.createNotificationHandler(context, (messageId, channelType, channelId) -> {
// Return the intent you want to be triggered when the notification is clicked
Intent intent = new Intent();
return intent;
});
new 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 onNotificationPermissionStatus(status: NotificationPermissionStatus) {
when (status) {
NotificationPermissionStatus.REQUESTED -> {
// invoked when POST_NOTIFICATIONS permission is requested
}
NotificationPermissionStatus.GRANTED -> {
// invoked when POST_NOTIFICATIONS permission is granted
}
NotificationPermissionStatus.DENIED -> {
// invoked when POST_NOTIFICATIONS permission is denied
}
NotificationPermissionStatus.RATIONALE_NEEDED -> {
// invoked when POST_NOTIFICATIONS permission requires rationale
}
}
}
override fun showNotification(channel: Channel, message: Message) {
val notification = 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
}
}
class MyNotificationHandler implements NotificationHandler {
NotificationManager notificationManager;
public MyNotificationHandler(Context context) {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE) ;
}
@Override
public void onNotificationPermissionStatus(@NonNull NotificationPermissionStatus status) {
switch (status) {
case REQUESTED:
// invoked when POST_NOTIFICATIONS permission is requested
break;
case GRANTED:
// invoked when POST_NOTIFICATIONS permission is granted
break;
case DENIED:
// invoked when POST_NOTIFICATIONS permission is denied
break;
case RATIONALE_NEEDED:
// invoked when POST_NOTIFICATIONS permission requires rationale
break;
}
}
@Override
public void showNotification(@NonNull Channel channel, @NonNull Message message) {
Notification notification = new NotificationCompat.Builder(context, notificationChannelId)
.build();
notificationManager.notify(notificationId, notification);
}
@Override
public void dismissChannelNotifications(@NonNull String channelType, @NonNull String channelId) {
// Dismiss all notification related with this channel
}
@Override
public void dismissAllNotifications() {
// Dismiss all notifications
}
@Override
public boolean onChatEvent(@NonNull NewMessageEvent event) {
return true;
}
@Override
public boolean onPushMessage(@NonNull PushMessage message) {
return false;
}
}
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()
NotificationHandler notificationHandler = new MyNotificationHandler(context);
new 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")
ChatClient.dismissChannelNotifications("messaging", "general");
Notifications are also automatically dismissed when the user logs out from the SDK.