Customize Push Notification Functionality for Android Apps
The Mobile App Messaging SDK offers extensive customization options for Android notifications through the NotificationCustomizationOptions class.
This example represents only the minimum configuration needed for displaying notifications on your Android apps with the SDK. For information on status bar icons, see Icons in Google’s Material Design documentation.
You can further customize push notifications by providing a Launch Intent Provider or specifying the action to be taken when a notification is tapped. For an example configuration, see Basic Customization.
You can also take complete control over push notifications, as described in the Full Control Customization section.
To facilitate additional customization such as deep linking or handling custom keys, use the NotificationMessage class.
The NotificationCustomizationOptions class facilitates push notification customization through a NotificationChannelIdProvider, enabling the assignment of a specific Notification Channel for each NotificationMessage.
Additionally, NotificationLaunchIntentProvider defines the action to take when a user taps a NotificationMessage, alongside your application’s notification icon.
When you target apps that run Android 12 or later, specify the mutability of your Pending Intent. See Android Developer Documentation: Intents and Intent Filters.
The Mobile App Messaging SDK supports icons in various notification elements, including small icons in the status bar and notification header, large icons in the notification body, and rich button icons.
You can use icons by placing icon files in the PNG format inside drawables-**hdpi folders within your client app’s resources. The SDK then references these icons by their resource names (for example, ic_notification_icon).
For example, to use an icon named ic_notification_icon.png, place it in the appropriate drawables-**hdpi folder. Then, provide ic_notification_icon as the resource name. If it’s a small icon, you can also specify the resource ID in NotificationCustomizationOptions when using MarketingCloudConfigBuilder.
You can customize notification channels by setting priorities, colors, and sounds. To use a custom sound in notifications, follow these steps:
- Add the sound file to the app’s
res/raw/folder. - Create a notification channel for the audio file and configure it in the code using
NotificationChannelIdProvider. This code example creates a notification channel namedmyaudio, enables badges and lights, and sets a default importance level.
This sample code configures the Mobile App Messaging SDK for an Android application. It sets up notification customization options, including the small icon, launch intent, and notification channel ID.
When providing a builder to the NotificationCustomizationOptions class, you’re responsible for managing all aspects of the notification’s display properties and tap actions, including Notification Channel creation, Launch Intent action, and so on.
You can access the NotificationCompat.Builder that would have been used by the SDK and use it as a baseline for modifications. Additionally, the SDK can create a default NotificationChannel for your application.
Marketers can configure the action when an end user taps a button or a carousel image in a push notification. The actions for Web URL and App URL require that you implement UrlHandler as a new SDK initialization method.
This example shows how to configure the SDK to handle button and carousel clicks in push messages.
On devices that run Android 8.0 or later, the SDK creates a default notification channel called marketing to assign all notifications to it. To change the name of the default channel, set a string resource value for mcsdk_default_notification_channel_name.
After the SDK creates the NotificationChannel, the channel isn’t updated unless your application calls NotificationManager#createDefaultNotificationChannel(Context context, boolean forceRecreate).
You can create custom notification channels and assign channels to each notification, as depicted by the following example configuration.
To assign a notification to a channel, implement the SDK NotificationChannelIdProvider interface. Ensure that you include a channel with your message. Otherwise, the app doesn’t display the message to the recipient.
You can customize channel priority, colors, and sounds. To use custom sounds, give each channel its own sound configuration while creating it. You can’t use sounds with the SDK default channel option.