Example usage for android.app NotificationManager getNotificationChannels

List of usage examples for android.app NotificationManager getNotificationChannels

Introduction

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

Prototype

public List<NotificationChannel> getNotificationChannels() 

Source Link

Document

Returns all notification channels belonging to the calling package.

Usage

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

public static boolean areNotificationsEnabled(@NonNull Context context,
        @NonNull AccountManager accountManager) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        // on Android >= O, notifications are enabled, if at least one channel is enabled
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        //noinspection ConstantConditions
        if (notificationManager.areNotificationsEnabled()) {
            for (NotificationChannel channel : notificationManager.getNotificationChannels()) {
                if (channel.getImportance() > NotificationManager.IMPORTANCE_NONE) {
                    Log.d(TAG, "NotificationsEnabled");
                    return true;
                }//from w  ww .ja v  a2  s  .c om
            }
        }
        Log.d(TAG, "NotificationsDisabled");

        return false;

    } else {
        // on Android < O, notifications are enabled, if at least one account has notification enabled
        return accountManager.areNotificationsEnabled();
    }

}

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

/**
 * Add a notification groups./*from w w  w.  jav  a  2  s . c  o  m*/
 *
 * @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);
    }
}

From source file:com.irccloud.android.data.collection.NotificationsList.java

public void pruneNotificationChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager nm = ((NotificationManager) IRCCloudApplication.getInstance()
                .getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE));

        for (NotificationChannelGroup c : nm.getNotificationChannelGroups()) {
            try {
                if (ServersList.getInstance().getServer(Integer.valueOf(c.getId())) == null)
                    nm.deleteNotificationChannelGroup(c.getId());
            } catch (NumberFormatException e) {
            }//from   w  w w  .  j a va2 s.c  o m
        }

        for (NotificationChannel c : nm.getNotificationChannels()) {
            try {
                if (BuffersList.getInstance().getBuffer(Integer.valueOf(c.getId())) == null)
                    nm.deleteNotificationChannel(c.getId());
            } catch (NumberFormatException e) {
            }
        }
    }
}