Skip to content

Notification settings

Alexander Boldyrev edited this page Oct 30, 2024 · 19 revisions

Mobile Messaging supports configuration of Notifications provided by the library. Different Notification settings are available via NotificationSettings.Builder.

Configure Notification Settings

Callback Activity

It is possible to define custom callback activity to open when user taps on notification. By default the library will use default activity for the application package.

NotificationSettings.Builder(context)
.withCallbackActivity(MyActivity::class.java)
.build()
expand to see Java code

new NotificationSettings.Builder(context)
.withCallbackActivity(MyActivity.class)
.build();

Notification Title

Default notification title is application name (R.string.app_name). However custom notification title can also be used in notifications.

NotificationSettings.Builder(this)
    .withDefaultTitle("MyCustomNotificationTitle")
    .build()
expand to see Java code

new NotificationSettings.Builder(this)
    .withDefaultTitle("MyCustomNotificationTitle")
    .build();

Note:

To force custom title and message body for In-App chat notifications please read Customize notifications

Notification Icon

Default notification icon is launcher icon (R.mipmap.ic_launcher). Custom icon resource id can also be supplied to overwrite default behavior. If supplied, the icon should follow guidelines described here: https://m2.material.io/design/iconography/system-icons.html#design-principles

NotificationSettings.Builder(this)
    .withDefaultIcon(R.drawable.ic_my_notification_icon)
    .build()
expand to see Java code

new NotificationSettings.Builder(this)
    .withDefaultIcon(R.drawable.ic_my_notification_icon)
    .build();

Notification accent color

Option for setting notification accent color is available since Android 6, API level 23.

NotificationSettings.Builder(this)
    .withColor(ContextCompat.getColor(this, R.color.red))
    .build()
expand to see Java code

new NotificationSettings.Builder(this)
    .withColor(ContextCompat.getColor(this, R.color.red))
    .build();

Multiple notifications

It is possible to show multiple notifications in status bar through NotificationSettings.Builder. By default, when new notification arrives, it shows single notification which is overwritten by the new one.

NotificationSettings.Builder(this)
.withMultipleNotifications()
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withMultipleNotifications()
.build();

Intent Flags

It is possible to configure notification intent flags. Provided flags are delegated to Intent.addFlags(). Default flags are Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.

NotificationSettings.Builder(this)
.withIntentFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withIntentFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.build();

Pending Intent Flags

It is also possible to set pending intent flags. The flags are delegated to PendingIntent.getActivity(). Default flag is PendingIntent.FLAG_CANCEL_CURRENT.

NotificationSettings.Builder(this)
.withPendingIntentFlags(0)
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withPendingIntentFlags(0)
.build();

Notification Auto-cancel

Notification auto-cancel parameter can be set via Notification Settings by calling an appropriate Builder method. The default value of auto-cancel is true. This parameter is forwarded to NotificationCompat.Builder.setAutoCancel().

NotificationSettings.Builder(this)
.withNotificationAutoCancel()
.build()

...

NotificationSettings.Builder(this)
.withoutNotificationAutoCancel()
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withNotificationAutoCancel()
.build();

...

new NotificationSettings.Builder(this)
.withoutNotificationAutoCancel()
.build();

Foreground Notifications

It is possible to configure library to not show notifications when your application is running in foreground. By default the library will show notifications always.

NotificationSettings.Builder(this)
.withoutForegroundNotification()
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withoutForegroundNotification()
.build();

Heads-up notifications

By default SDK will show Heads-up notifications on devices running Android 5.0+. Such notifications will only be shown when the app is in background or killed. You can disable heads-up notifications using method described below.

NotificationSettings.Builder(this)
.withoutHeadsUpNotifications()
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withoutHeadsUpNotifications()
.build();

Modal mirror push notifications

By default SDK will show Modal mirror push notifications. You can disable modal mirror push notifications using method described below.

NotificationSettings.Builder(this)
.withoutModalInAppNotifications()
.build()
expand to see Java code

new NotificationSettings.Builder(this)
.withoutModalInAppNotifications()
.build();

Set Notification Settings

Constructed Notification Settings can be applied via MobileMessaging.Builder.

val mobileMessaging = MobileMessaging.Builder(application)
                .withDisplayNotification(NotificationSettings.Builder(application)
                    .withoutNotificationAutoCancel()
                    .withoutForegroundNotification()
                    .build())
                .build()
expand to see Java code

MobileMessaging mobileMessaging = new MobileMessaging.Builder(application)
                .withDisplayNotification(new NotificationSettings.Builder(application)
                    .withoutNotificationAutoCancel()
                    .withoutForegroundNotification()
                    .build())
                .build();
Mobile Messaging library can also be configured to work without notifications.
val mobileMessaging = MobileMessaging.Builder(application)
                .withoutDisplayNotification()
                .build()
expand to see Java code

MobileMessaging mobileMessaging = new MobileMessaging.Builder(application)
                .withoutDisplayNotification()
                .build();
Clone this wiki locally