Implement Default and Custom Notification Channels for the Android SDK
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 shown in these code examples.
1val channelId = "my_custom_channel"
2
3if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
4 (getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager).createNotificationChannel(
5 NotificationChannel(
6 channelId,
7 "Marketing Messages",
8 android.app.NotificationManager.IMPORTANCE_DEFAULT
9 )
10 )
11}
12
13SFMCSdk.configure(applicationContext as Application, SFMCSdkModuleConfig.build {
14 pushModuleConfig = MarketingCloudConfig.builder().apply {
15 // Other configuration options
16 setNotificationCustomizationOptions(
17 NotificationCustomizationOptions.create(
18 R.drawable.ic_notification_icon,
19 null,
20 NotificationManager.NotificationChannelIdProvider { context, notificationMessage ->
21 // Whatever custom logic required to determine which channel should be used for the message.
22 channelId
23 }
24 )
25 )
26 }.build(applicationContext)
27}) {
28 // TODO handle initialization status
29}
1val channelId = "my_custom_channel"
2
3if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
4 (getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager).createNotificationChannel(
5 NotificationChannel(
6 channelId,
7 "Marketing Messages",
8 android.app.NotificationManager.IMPORTANCE_DEFAULT
9 )
10 )
11}
12
13MarketingCloudSdk.init(applicationContext as Application, MarketingCloudConfig.builder().apply {
14 // Other configuration options
15 setNotificationCustomizationOptions(
16 NotificationCustomizationOptions.create(
17 R.drawable.ic_notification_icon,
18 null,
19 NotificationManager.NotificationChannelIdProvider { context, notificationMessage ->
20 // Whatever custom logic required to determine which channel should be used for the message.
21 channelId
22 }
23 )
24 )
25}.build(applicationContext)) {
26 // TODO handle initialization status
27}
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.