Example usage for android.app NotificationManager createNotificationChannels

List of usage examples for android.app NotificationManager createNotificationChannels

Introduction

In this page you can find the example usage for android.app NotificationManager createNotificationChannels.

Prototype

public void createNotificationChannels(@NonNull List<NotificationChannel> channels) 

Source Link

Document

Creates multiple notification channels that different notifications can be posted to.

Usage

From source file:com.keylesspalace.tusky.util.NotificationHelper.java

public static void createNotificationChannelsForAccount(@NonNull AccountEntity account,
        @NonNull Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        String[] channelIds = new String[] { CHANNEL_MENTION + account.getIdentifier(),
                CHANNEL_FOLLOW + account.getIdentifier(), CHANNEL_BOOST + account.getIdentifier(),
                CHANNEL_FAVOURITE + account.getIdentifier() };
        int[] channelNames = { R.string.notification_channel_mention_name,
                R.string.notification_channel_follow_name, R.string.notification_channel_boost_name,
                R.string.notification_channel_favourite_name };
        int[] channelDescriptions = { R.string.notification_channel_mention_descriptions,
                R.string.notification_channel_follow_description,
                R.string.notification_channel_boost_description,
                R.string.notification_channel_favourite_description };

        List<NotificationChannel> channels = new ArrayList<>(4);

        NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(),
                account.getFullName());/*  w  ww .jav  a  2s  .  c  o  m*/

        //noinspection ConstantConditions
        notificationManager.createNotificationChannelGroup(channelGroup);

        for (int i = 0; i < channelIds.length; i++) {
            String id = channelIds[i];
            String name = context.getString(channelNames[i]);
            String description = context.getString(channelDescriptions[i]);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(id, name, importance);

            channel.setDescription(description);
            channel.enableLights(true);
            channel.setLightColor(0xFF2B90D9);
            channel.enableVibration(true);
            channel.setShowBadge(true);
            channel.setGroup(account.getIdentifier());
            channels.add(channel);
        }

        //noinspection ConstantConditions
        notificationManager.createNotificationChannels(channels);

    }
}