Basic Push Notification Customization for Android

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.

Note

1SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
2  this.pushFeatureModuleConfig = PushFeatureConfig.builder().apply {
3    // Other configuration values
4    setNotificationCustomizationOptions(
5      NotificationCustomizationOptions.create(R.drawable.ic_notification_icon,
6        NotificationManager.NotificationLaunchIntentProvider { context, notificationMessage ->
7          val requestCode = Random().nextInt()
8          val url = notificationMessage.url
9          when {
10              url.isNullOrEmpty() ->
11                  PendingIntent.getActivity(
12                    context,
13                    requestCode,
14                    Intent(context, MainActivity::class.java),
15                    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
16                  )
17              else ->
18                  PendingIntent.getActivity(
19                    context,
20                    requestCode,
21                    Intent(Intent.ACTION_VIEW, url.toUri()),
22                    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
23                  )
24          }
25        },
26        NotificationManager.NotificationChannelIdProvider { context, notificationMessage ->
27          if (TextUtils.isEmpty(notificationMessage.url)) {
28            NotificationManager.createDefaultNotificationChannel(context)
29          } else {
30            "UrlNotification"
31          }
32        }
33      )
34    )
35  }.build()
36  })
37{
38  // Handle initialization status
39}

Icons 

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.

Sound 

You can customize notification channels by setting priorities, colors, and sounds. To use a custom sound in notifications, follow these steps:

  1. Add the sound file to the app’s res/raw/ folder.
  2. Create a notification channel for the audio file and configure it in the code using NotificationChannelIdProvider. This code example creates a notification channel named myaudio, enables badges and lights, and sets a default importance level.
1/*
2Create a notification channel for the audio file
3*/
4if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
5  androidNotificationManager.createNotificationChannel(
6    NotificationChannel(
7      "myaudio", "myaudio",
8      AndroidNotificationManager.IMPORTANCE_DEFAULT
9    ).apply {
10      setShowBadge(true)
11      enableLights(true)
12      setSound(
13        Uri.parse(
14          "android.resource://${context.packageName}/${context.resources.getIdentifier("custom","raw", context.packageName)}"
15        ),
16        AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT).build()
17      )
18    }
19  )
20}

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.

1SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
2  this.pushFeatureModuleConfig = PushFeatureConfig.builder().apply {
3    // Other configuration values
4    setNotificationCustomizationOptions(
5      NotificationCustomizationOptions.create(R.drawable.ic_notification_icon,
6        NotificationManager.NotificationLaunchIntentProvider { context, notificationMessage ->
7          val requestCode = Random().nextInt()
8          val url = notificationMessage.url
9          when {
10            url.isNullOrEmpty() ->
11              PendingIntent.getActivity(
12                context,
13                requestCode,
14                Intent(context, MainActivity::class.java),
15                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
16              )
17            else ->
18              PendingIntent.getActivity(
19                context,
20                requestCode,
21                Intent(Intent.ACTION_VIEW, url.toUri()),
22                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
23              )
24          }
25        },
26        NotificationManager.NotificationChannelIdProvider { context, notificationMessage ->
27          if (TextUtils.isEmpty(notificationMessage.url)) {
28            NotificationManager.createDefaultNotificationChannel(context)
29          } else if(notificationMessage.sound == NotificationMessage.Sound.CUSTOM) {
30            "CustomSoundNotification"
31          } else {
32            "UrlNotification"
33          }
34        }
35      )
36    )
37  }.build()
38}) {
39  // Handle initialization status
40}