Example usage for android.app NotificationChannel setShowBadge

List of usage examples for android.app NotificationChannel setShowBadge

Introduction

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

Prototype

public void setShowBadge(boolean showBadge) 

Source Link

Document

Sets whether notifications posted to this channel can appear as application icon badges in a Launcher.

Usage

From source file:com.tourmaline.example.helpers.Alerts.java

public static void show(final Context context, final Type type) {
    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = "";
    String channelTitle = "";
    String notifTitle = "";
    String notifText = context.getString(R.string.app_name) + " "
            + context.getString(R.string.permission_notif_text);
    int smallIconRes = R.mipmap.ic_warning_white;
    int largeIconRes = 0;
    int notifId = 0;

    switch (type) {
    case GPS: {/*from www.  ja v a2  s .  c o  m*/
        channelId = NOTIF_CHANNEL_ID_GPS;
        channelTitle = context.getString(R.string.gps_notif_title);
        notifTitle = context.getString(R.string.gps_notif_title);
        largeIconRes = R.mipmap.ic_location_off_black;
        notifId = NOTIF_ID_GPS;
        break;
    }
    case PERMISSION: {
        channelId = NOTIF_CHANNEL_ID_PERMISSION;
        channelTitle = context.getString(R.string.permission_notif_title);
        notifTitle = context.getString(R.string.permission_notif_title);
        largeIconRes = R.mipmap.ic_location_off_black;
        notifId = NOTIF_ID_PERMISSION;
        break;
    }
    case POWER: {
        channelId = NOTIF_CHANNEL_ID_POWER;
        channelTitle = context.getString(R.string.power_notif_title);
        notifTitle = context.getString(R.string.power_notif_title);
        largeIconRes = R.mipmap.ic_battery_alert_black;
        notifId = NOTIF_ID_POWER;
        break;
    }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final NotificationChannel channel = new NotificationChannel(channelId, channelTitle,
                NotificationManager.IMPORTANCE_HIGH);
        channel.setShowBadge(true);
        notificationManager.createNotificationChannel(channel);
    }

    final Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), largeIconRes);

    final Notification note = new NotificationCompat.Builder(context, channelId).setContentTitle(notifTitle)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(notifText)).setSmallIcon(smallIconRes)
            .setLargeIcon(largeIcon).setVisibility(VISIBILITY_SECRET)
            .setPriority(NotificationCompat.PRIORITY_MAX).build();

    notificationManager.notify(notifId, note);

}

From source file:com.none.tom.simplerssreader.service.FeedUpdateBackgroundService.java

@SuppressWarnings({ "ConstantConditions", "deprecation" })
private static void showNotification(final Context context, final List<String> payload) {
    final NotificationManager manager = context.getSystemService(NotificationManager.class);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setBypassDnd(false);/*from w  ww. ja  v  a2  s .  c  om*/
        channel.enableLights(true);
        channel.setShowBadge(true);
        channel.enableVibration(true);

        manager.createNotificationChannel(channel);
    }

    final PendingIntent intent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
            PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
            .setBigContentTitle(context.getString(R.string.notification_title_big));

    final int size = payload.size();

    for (int i = 0; i < size; i++) {
        style.addLine(payload.get(i));
    }

    manager.notify(ID_NOTIFICATION, new NotificationCompat.Builder(context)
            .setChannelId(NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_rss_feed_white_24dp)
            .setContentTitle(context.getString(R.string.notification_title))
            .setContentText(context.getString(R.string.notification_text)).setContentIntent(intent)
            .setStyle(style).setWhen(System.currentTimeMillis()).setAutoCancel(true).setShowWhen(true).build());
}

From source file:com.ubergeek42.WeechatAndroid.service.Notificator.java

@MainThread
public static void init(Context c) {
    context = c.getApplicationContext();
    manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
        return;//from  w w  w  .  ja  v  a2 s.com

    NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_CONNECTION_STATUS,
            context.getString(R.string.notification_channel_connection_status),
            NotificationManager.IMPORTANCE_MIN);
    channel.setShowBadge(false);
    manager.createNotificationChannel(channel);

    channel = new NotificationChannel(NOTIFICATION_CHANNEL_HOTLIST,
            context.getString(R.string.notification_channel_hotlist), NotificationManager.IMPORTANCE_DEFAULT);
    manager.createNotificationChannel(channel);
}

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

public static void createNotificationChannels(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        android.app.NotificationManager mNotificationManager = (android.app.NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        String[] channelIds = new String[] { CHANNEL_MENTION, CHANNEL_FOLLOW, CHANNEL_BOOST,
                CHANNEL_FAVOURITE };//  w  ww  . j a v  a  2  s . com
        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);

        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 = android.app.NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(id, name, importance);

            channel.setDescription(description);
            channel.enableLights(true);
            channel.enableVibration(true);
            channel.setShowBadge(true);
            channels.add(channel);
        }

        //noinspection ConstantConditions
        mNotificationManager.createNotificationChannels(channels);

    }
}

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
 *//*from w w w  .j  av 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: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());//from  ww w  .j a v a 2 s . co  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);

    }
}

From source file:com.ruesga.rview.misc.NotificationsHelper.java

@TargetApi(Build.VERSION_CODES.O)
public static void createNotificationChannel(Context context, Account account) {
    if (AndroidHelper.isApi26OrGreater()) {
        final String defaultChannelName = context.getString(R.string.notifications_default_channel_name,
                account.getRepositoryDisplayName(), account.getAccountDisplayName());
        final NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        nm.createNotificationChannelGroup(
                new NotificationChannelGroup(account.getAccountHash(), defaultChannelName));

        NotificationChannel channel = new NotificationChannel(account.getAccountHash(), defaultChannelName,
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(context.getString(R.string.notifications_default_channel_description));
        channel.enableVibration(true);//from  w w w  . j a va  2  s .  co m
        channel.enableLights(true);
        channel.setLightColor(ContextCompat.getColor(context, R.color.primaryDark));
        channel.setShowBadge(true);
        channel.setGroup(account.getAccountHash());
        nm.createNotificationChannel(channel);
    }
}

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

@TargetApi(Build.VERSION_CODES.O)
public static void createPersistentNotificationChannel(Context context) {
    removeOldPersistentNotificationFromChannel(context);
    NotificationChannel channel = new NotificationChannel(AppConstant.YONA_SERVICE_CHANNEL_ID,
            context.getString(R.string.yona_service_notification_channel_name),
            NotificationManager.IMPORTANCE_MIN);
    channel.setShowBadge(false);
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
            .createNotificationChannel(channel);
}

From source file:com.commonsware.android.notify.channel.MainActivity.java

private void initBattleChannel() {
    NotificationChannel channel = new NotificationChannel(CHANNEL_BATTLE,
            getString(R.string.channel_name_battle), NotificationManager.IMPORTANCE_HIGH);

    channel.setGroup(GROUP_UPDATES);/*  ww w.jav a 2s . c om*/
    channel.setShowBadge(true);
    mgr.createNotificationChannel(channel);
}

From source file:com.davidmiguel.gobees.utils.NotificationsHelper.java

@RequiresApi(Build.VERSION_CODES.O)
private void createMonitoringChannel() {
    NotificationChannel channel = new NotificationChannel(MONITORING_CHANNEL,
            getString(R.string.not_channel_monitoring), NotificationManager.IMPORTANCE_LOW);
    channel.setDescription(getString(R.string.not_channel_monitoring_desc));
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    channel.setShowBadge(false);
    channel.enableLights(false);/*  w w  w  .  ja v a 2 s  .c o m*/
    getManager().createNotificationChannel(channel);
}