Example usage for android.app NotificationManager getNotificationChannel

List of usage examples for android.app NotificationManager getNotificationChannel

Introduction

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

Prototype

public NotificationChannel getNotificationChannel(String channelId) 

Source Link

Document

Returns the notification channel settings for a given channel id.

Usage

From source file:com.commonsware.android.sawmonitor.SAWDetector.java

static void seeSAW(Context ctxt, String pkg, String operation) {
    if (hasSAW(ctxt, pkg)) {
        Uri pkgUri = Uri.parse("package:" + pkg);
        Intent manage = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);

        manage.setData(pkgUri);//from  www.  j  ava2  s. c o m

        Intent whitelist = new Intent(ctxt, WhitelistReceiver.class);

        whitelist.setData(pkgUri);

        Intent main = new Intent(ctxt, MainActivity.class);

        main.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        NotificationManager mgr = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                && mgr.getNotificationChannel(CHANNEL_WHATEVER) == null) {
            mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER, "Whatever",
                    NotificationManager.IMPORTANCE_DEFAULT));
        }

        NotificationCompat.Builder b = new NotificationCompat.Builder(ctxt, CHANNEL_WHATEVER);
        String text = String.format(ctxt.getString(R.string.msg_requested), operation, pkg);

        b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis())
                .setContentTitle(ctxt.getString(R.string.msg_detected)).setContentText(text)
                .setSmallIcon(android.R.drawable.stat_notify_error)
                .setTicker(ctxt.getString(R.string.msg_detected))
                .setContentIntent(PendingIntent.getActivity(ctxt, 0, manage, PendingIntent.FLAG_UPDATE_CURRENT))
                .addAction(R.drawable.ic_verified_user_24dp, ctxt.getString(R.string.msg_whitelist),
                        PendingIntent.getBroadcast(ctxt, 0, whitelist, 0))
                .addAction(R.drawable.ic_settings_24dp, ctxt.getString(R.string.msg_settings),
                        PendingIntent.getActivity(ctxt, 0, main, 0));

        mgr.notify(NOTIFY_ID, b.build());
    }
}

From source file:com.google.android.apps.muzei.notifications.NewWallpaperNotificationReceiver.java

static boolean isNewWallpaperNotificationEnabled(@NonNull Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // On O+ devices, we defer to the system setting
        if (!createNotificationChannel(context)) {
            // Don't post the new wallpaper notification in the case where
            // we've also posted the 'Review your settings' notification
            return false;
        }//from  w  ww .  ja  v a  2  s  . c om
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return false;
        }
        NotificationChannel channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL);
        return channel != null && channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
    }
    // Prior to O, we maintain our own preference
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    return sp.getBoolean(PREF_ENABLED, true);
}

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

private static boolean filterNotification(AccountEntity account, Notification notification, Context context) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        //noinspection ConstantConditions
        NotificationChannel channel = notificationManager
                .getNotificationChannel(getChannelId(account, notification));
        return channel.getImportance() > NotificationManager.IMPORTANCE_NONE;
    }// w ww  .  ja  v a2s.  c  om

    switch (notification.getType()) {
    default:
    case MENTION:
        return account.getNotificationsMentioned();
    case FOLLOW:
        return account.getNotificationsFollowed();
    case REBLOG:
        return account.getNotificationsReblogged();
    case FAVOURITE:
        return account.getNotificationsFavorited();
    }
}

From source file:nu.yona.app.utils.AppUtils.java

@TargetApi(Build.VERSION_CODES.O)
public static NotificationChannel getPersistentNotificationChannel(Context context) {
    android.app.NotificationManager notificationManager = context
            .getSystemService(android.app.NotificationManager.class);
    NotificationChannel notificationChannel = notificationManager
            .getNotificationChannel(AppConstant.YONA_SERVICE_CHANNEL_ID);
    return notificationChannel;
}

From source file:com.commonsware.android.bluetooth.rxecho.ShoutingEchoService.java

@Override
public void onCreate() {
    super.onCreate();

    rxBluetooth = new RxBluetooth(getApplicationContext());
    acceptConnections();//from w w  w. java  2s  .c o  m

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
            && mgr.getNotificationChannel(CHANNEL_WHATEVER) == null) {
        mgr.createNotificationChannel(
                new NotificationChannel(CHANNEL_WHATEVER, "Whatever", NotificationManager.IMPORTANCE_DEFAULT));
    }

    startForeground(1338, buildForegroundNotification());

    STATUS.postValue(Status.IS_RUNNING);
}

From source file:org.LK8000.MyService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    /* add an icon to the notification area while LK8000 runs, to
    remind the user that we're sucking his battery empty */

    final String CHANNEL_ID = getApplicationContext().getPackageName() + "_NotificationChannel";

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Support for Android Oreo: Notification Channels
        NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
        if (channel == null) {
            channel = new NotificationChannel(CHANNEL_ID, "LK8000", NotificationManager.IMPORTANCE_LOW);

            manager.createNotificationChannel(channel);
        }/*from  w  w  w.  j  av a  2 s.  c o m*/
    }

    Intent intent2 = new Intent(this, mainActivityClass);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent2, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID);
    notification.setSmallIcon(R.drawable.notification_icon);
    notification.setLargeIcon(BitmapFactory.decodeResource(getResources(), getApplicationInfo().icon));
    notification.setContentTitle("LK8000 is running");
    notification.setContentText("Touch to open");
    notification.setContentIntent(contentIntent);
    notification.setWhen(System.currentTimeMillis());
    notification.setShowWhen(false);
    notification.setOngoing(true);
    notification.setOnlyAlertOnce(true);

    startForeground(1, notification.build());

    /* We want this service to continue running until it is explicitly
       stopped, so return sticky */
    return START_STICKY;
}

From source file:com.google.android.apps.muzei.notifications.NewWallpaperNotificationReceiver.java

/**
 * Create the notification channel for the New Wallpaper notification
 * @return False only in the case where the user had wallpapers disabled in-app, but has not
 * yet seen the 'Review your notification settings' notification
 *///  w  ww  . ja v  a 2 s.  c  o m
@RequiresApi(api = Build.VERSION_CODES.O)
static boolean createNotificationChannel(Context context) {
    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    // On O+ devices, we want to push users to change the system notification setting
    // but we'll use their current value to set the default importance
    int defaultImportance = sp.getBoolean(PREF_ENABLED, true) ? NotificationManager.IMPORTANCE_MIN
            : NotificationManager.IMPORTANCE_NONE;
    if (sp.contains(PREF_ENABLED)) {
        sp.edit().remove(PREF_ENABLED).apply();
        if (defaultImportance == NotificationManager.IMPORTANCE_NONE) {
            // Check to see if there was already a channel and give users an
            // easy way to review their notification settings if they had
            // previously disabled notifications but have not yet disabled
            // the channel
            NotificationChannel existingChannel = notificationManager
                    .getNotificationChannel(NOTIFICATION_CHANNEL);
            if (existingChannel != null
                    && existingChannel.getImportance() != NotificationManager.IMPORTANCE_NONE) {
                // Construct an Intent to get to the notification settings screen
                Intent settingsIntent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                settingsIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                settingsIntent.putExtra(Settings.EXTRA_CHANNEL_ID,
                        NewWallpaperNotificationReceiver.NOTIFICATION_CHANNEL);
                // Build the notification
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
                        NOTIFICATION_CHANNEL).setSmallIcon(R.drawable.ic_stat_muzei)
                                .setColor(ContextCompat.getColor(context, R.color.notification))
                                .setAutoCancel(true)
                                .setContentTitle(context.getText(R.string.notification_settings_moved_title))
                                .setContentText(context.getText(R.string.notification_settings_moved_text))
                                .setContentIntent(PendingIntent.getActivity(context, 0, settingsIntent,
                                        PendingIntent.FLAG_UPDATE_CURRENT))
                                .setStyle(new NotificationCompat.BigTextStyle()
                                        .bigText(context.getText(R.string.notification_settings_moved_text)));
                notificationManager.notify(1, builder.build());
                return false;
            }
        }
    }
    NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL,
            context.getString(R.string.notification_new_wallpaper_channel_name), defaultImportance);
    channel.setShowBadge(false);
    notificationManager.createNotificationChannel(channel);
    return true;
}

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

/**
 * Add a notification groups.//from  w  w  w  .j  av  a2  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);
    }
}