Android: Notification Channels

Customizing your default channel

If you want to provide custom behavior for your notifications - eg. Notification lights, vibrate patterns, custom sound effects, etc, then on devices using Android 8.0 you should do this through the NotificationChannel rather than through Marigold's NotificationConfig class. If you're targeting versions below 8.0 (API Level 26), then you should probably provide fallback configuration using NotificationConfig. This can be done like so:

NotificationConfig config = new NotificationConfig();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  	NotificationChannel channel = new NotificationChannel("notifications_default", "Marigold Notifications", NotificationManager.IMPORTANCE_DEFAULT);
    // Channel config for devices where android version >= 8.0.0
  	channel.enableLights(true);
  	channel.setLightColor(Color.RED);
  	channel.enableVibration(true);
  	channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  	config.setDefaultNotificationChannel(channel);
}

// Fallback config for devices where android version < 8.0.0
config.setLights(Color.RED, 500, 500);
config.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

Marigold marigold = new Marigold();
marigold.setNotificationConfig(config);
marigold.startEngine(getApplicationContext(), "SDK_KEY");
val config = NotificationConfig()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel("notifications_default", "Marigold Notifications", NotificationManager.IMPORTANCE_DEFAULT)
    // Channel config for devices where android version >= 8.0.0
    channel.enableLights(true)
    channel.lightColor = Color.RED
    channel.enableVibration(true)
    channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
    config.setDefaultNotificationChannel(channel)
}

// Fallback config for devices where android version < 8.0.0
config.setLights(Color.RED, 500, 500)
config.setVibrate(longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400))

val marigold = Marigold()
marigold.setNotificationConfig(config)
marigold.startEngine(applicationContext, "SDK_KEY")

This means that notifications will have the same behavior on both pre- and post-Oreo devices.

Note that the nature of channels is that developers can only set presets - this is how your channel will start when your user installs the app. After that, they'll be able to change any and all of the settings that you've set above.

Using multiple channels

The addition of channels in Android Oreo means that it's easier for you to split notifications out into categories, and to alert users in specific ways for specific reasons.

For example, a news app might create channels for breaking news, political news, and weather. These can all be set up differently so that the user gets different kinds of notifications for each channel, as illustrated below

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel breaking = new NotificationChannel("breaking", "Breaking News", NotificationManager.IMPORTANCE_HIGH);
    breaking.enableLights(true);
    breaking.enableVibration(true);
    breaking.setLightColor(Color.WHITE);
    breaking.setVibrationPattern(new long[]{100, 200, 100, 200, 100, 200, 100});

    config.setDefaultNotificationChannel(breaking);

    NotificationChannel politics = new NotificationChannel("politics", "Political News", NotificationManager.IMPORTANCE_DEFAULT);
    politics.enableLights(true);
    politics.enableVibration(true);
    politics.setLightColor(Color.BLUE);
    politics.setVibrationPattern(new long[]{100, 200, 100, 200, 100});

    NotificationChannel weather = new NotificationChannel("weather", "Weather Updates", NotificationManager.IMPORTANCE_DEFAULT);
    weather.enableLights(true);
    weather.enableVibration(true);
    weather.setLightColor(Color.GREEN);
    weather.setVibrationPattern(new long[]{100, 200, 100, 150, 100});

    NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(breaking);
    notificationManager.createNotificationChannel(politics);
    notificationManager.createNotificationChannel(weather);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val breaking = NotificationChannel("breaking", "Breaking News", NotificationManager.IMPORTANCE_HIGH)
    breaking.enableLights(true)
    breaking.enableVibration(true)
    breaking.lightColor = Color.WHITE
    breaking.vibrationPattern = longArrayOf(100, 200, 100, 200, 100, 200, 100)
    config.setDefaultNotificationChannel(breaking)
    val politics = NotificationChannel("politics", "Political News", NotificationManager.IMPORTANCE_DEFAULT)
    politics.enableLights(true)
    politics.enableVibration(true)
    politics.lightColor = Color.BLUE
    politics.vibrationPattern = longArrayOf(100, 200, 100, 200, 100)
    val weather = NotificationChannel("weather", "Weather Updates", NotificationManager.IMPORTANCE_DEFAULT)
    weather.enableLights(true)
    weather.enableVibration(true)
    weather.lightColor = Color.GREEN
    weather.vibrationPattern = longArrayOf(100, 200, 100, 150, 100)
    val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(breaking)
    notificationManager.createNotificationChannel(politics)
    notificationManager.createNotificationChannel(weather)
}

Note that we've set the breaking channel as the Marigold default one on line 9. This means that any messages sent without channel IDs, or with ones that don't match those on the device being sent to, will be sent through the breaking channel.

Sending to specific channels

As mentioned above, if you send a message without a channel ID specified, it'll go through your default channel. If you'd like to send to a specific channel, however, it can be done easily through both the Marigold user interface and APIs.