Example usage for android.app NotificationChannel getImportance

List of usage examples for android.app NotificationChannel getImportance

Introduction

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

Prototype

public int getImportance() 

Source Link

Document

Returns the user specified importance e.g.

Usage

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 www . jav a 2 s  .c o  m*/
        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.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   ww w  . 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: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;
                }/* w  ww. j av  a 2 s.c  o m*/
            }
        }
        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: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;
    }//from ww  w.j  ava  2  s.c o m

    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:com.sxt.chat.activity.NotifycationActivity.java

/**
 * ??//from ww w  . j ava  2 s.c o m
 *
 * @param id ID
 */
public void sendNotification(int id, final String body) {

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel hahahah = helper.getManager().getNotificationChannel(DEFAULT_CHANNEL);
        Log.e("NotificationChannel", String.format("importance %s , flag %s",
                hahahah.getImportance() == NotificationManager.IMPORTANCE_NONE, hahahah.getImportance()));

    }

    final NotificationCompat.Action action = new NotificationCompat.Action(
            R.drawable.ic_ar_photo_main_blue_24dp, String.valueOf("~"),
            getPendingIntent(this, MainActivity.class));
    switch (id) {
    case NOTI_1:
        helper.notify(NOTI_ID_1--, helper.buildNotificationText(" Title", body,
                getPendingIntent(this, MainActivity.class), action, action, action));
        break;

    case NOTI_2:
        Glide.with(this).load(uri != null ? uri : URL1).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap source, GlideAnimation<? super Bitmap> glideAnimation) {
                //                                float radius = Resources.getSystem().getDisplayMetrics().density * 8;
                //                                Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
                //                                Canvas canvas = new Canvas(bitmap);
                //                                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                //                                paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
                //                                RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
                //                                canvas.drawRoundRect(rectF, radius, radius, paint);
                //
                //                                ImageView img = findViewById(R.id.img);
                //                                img.setImageBitmap(bitmap);
                helper.notify(NOTI_ID_2++, helper.buildNotificationImage(" Title", body, source,
                        getPendingIntent(App.getCtx(), MainActivity.class)));
            }
        });

        break;

    case NOTI_3:
        helper.notify(NOTI_ID_3, helper.buildCustomNotificationDefault("  Title", body,
                getPendingIntent(this, MainActivity.class)));
        break;
    }
}