Example usage for android.app NotificationChannel getSound

List of usage examples for android.app NotificationChannel getSound

Introduction

In this page you can find the example usage for android.app NotificationChannel getSound.

Prototype

public Uri getSound() 

Source Link

Document

Returns the notification sound for this channel.

Usage

From source file:im.vector.notifications.NotificationUtils.java

/**
 * Add a notification groups./*w  w  w  .ja v  a  2 s  .  c om*/
 *
 * @param context the context
 */
@SuppressLint("NewApi")
public static void addNotificationChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }

    if (null == NOISY_NOTIFICATION_CHANNEL_NAME) {
        NOISY_NOTIFICATION_CHANNEL_NAME = context.getString(R.string.notification_noisy_notifications);
    }

    if (null == SILENT_NOTIFICATION_CHANNEL_NAME) {
        SILENT_NOTIFICATION_CHANNEL_NAME = context.getString(R.string.notification_silent_notifications);
    }

    if (null == CALL_NOTIFICATION_CHANNEL_NAME) {
        CALL_NOTIFICATION_CHANNEL_NAME = context.getString(R.string.call);
    }

    if (null == LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME) {
        LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME = context
                .getString(R.string.notification_listen_for_events);
    }

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

    // A notification channel cannot be updated :
    // it must be deleted and created with another channel id
    if ((null == NOISY_NOTIFICATION_CHANNEL_ID)) {
        List<NotificationChannel> channels = notificationManager.getNotificationChannels();

        for (NotificationChannel channel : channels) {
            if (channel.getId().startsWith(NOISY_NOTIFICATION_CHANNEL_ID_BASE)) {
                NOISY_NOTIFICATION_CHANNEL_ID = channel.getId();
            }
        }
    }

    if (null != NOISY_NOTIFICATION_CHANNEL_ID) {
        NotificationChannel channel = notificationManager.getNotificationChannel(NOISY_NOTIFICATION_CHANNEL_ID);
        Uri notificationSound = channel.getSound();
        Uri expectedSound = PreferencesManager.getNotificationRingTone(context);

        // the notification sound has been updated
        // need to delete it, to create a new one
        // else the sound won't be updated
        if (((null == notificationSound) ^ (null == expectedSound)) || ((null != notificationSound)
                && !TextUtils.equals(notificationSound.toString(), expectedSound.toString()))) {
            notificationManager.deleteNotificationChannel(NOISY_NOTIFICATION_CHANNEL_ID);
            NOISY_NOTIFICATION_CHANNEL_ID = null;
        }
    }

    if (null == NOISY_NOTIFICATION_CHANNEL_ID) {
        NOISY_NOTIFICATION_CHANNEL_ID = NOISY_NOTIFICATION_CHANNEL_ID_BASE + System.currentTimeMillis();

        NotificationChannel channel = new NotificationChannel(NOISY_NOTIFICATION_CHANNEL_ID,
                NOISY_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(NOISY_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(PreferencesManager.getNotificationRingTone(context), null);
        channel.enableVibration(true);
        notificationManager.createNotificationChannel(channel);
    }

    if (null == notificationManager.getNotificationChannel(SILENT_NOTIFICATION_CHANNEL_NAME)) {
        NotificationChannel channel = new NotificationChannel(SILENT_NOTIFICATION_CHANNEL_ID,
                SILENT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(SILENT_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }

    if (null == notificationManager.getNotificationChannel(LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_ID)) {
        NotificationChannel channel = new NotificationChannel(LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_ID,
                LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_MIN);
        channel.setDescription(LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }

    if (null == notificationManager.getNotificationChannel(CALL_NOTIFICATION_CHANNEL_ID)) {
        NotificationChannel channel = new NotificationChannel(CALL_NOTIFICATION_CHANNEL_ID,
                CALL_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(CALL_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }
}