Example usage for android.app NotificationChannel NotificationChannel

List of usage examples for android.app NotificationChannel NotificationChannel

Introduction

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

Prototype

public NotificationChannel(String id, CharSequence name, @Importance int importance) 

Source Link

Document

Creates a notification channel.

Usage

From source file:com.android.tv.tuner.setup.TunerSetupActivity.java

private static void sendNotificationInternal(Context context, String contentTitle, String contentText) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(new NotificationChannel(TUNER_SET_UP_NOTIFICATION_CHANNEL_ID,
            context.getResources().getString(R.string.ut_setup_notification_channel_name),
            NotificationManager.IMPORTANCE_HIGH));
    Notification notification = new Notification.Builder(context, TUNER_SET_UP_NOTIFICATION_CHANNEL_ID)
            .setContentTitle(contentTitle).setContentText(contentText)
            .setSmallIcon(/*from   www .  j  av  a  2s .c o m*/
                    context.getResources().getIdentifier(TAG_ICON, TAG_DRAWABLE, context.getPackageName()))
            .setContentIntent(createPendingIntentForSetupActivity(context))
            .setVisibility(Notification.VISIBILITY_PUBLIC).extend(new Notification.TvExtender()).build();
    notificationManager.notify(NOTIFY_TAG, NOTIFY_ID, notification);
}

From source file:github.popeen.dsub.util.Notifications.java

@TargetApi(Build.VERSION_CODES.O)
private static NotificationChannel getPlayingNotificationChannel(Context context) {
    if (playingChannel == null) {
        playingChannel = new NotificationChannel("now-playing-channel", "Now Playing",
                NotificationManager.IMPORTANCE_LOW);
        playingChannel.setDescription("Now playing notification");

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(playingChannel);
    }//from w  w  w .  ja v  a 2 s .c  om

    return playingChannel;
}

From source file:com.scooter1556.sms.android.manager.MediaNotificationManager.java

/**
 * Creates Notification Channel. This is required in Android O+ to display notifications.
 *//*w  ww.  ja  v a  2  s . c  o  m*/
private void createNotificationChannel() {
    if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
                mediaService.getString(R.string.notification_channel), NotificationManager.IMPORTANCE_LOW);

        notificationChannel.setDescription(mediaService.getString(R.string.notification_channel_description));
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

From source file:com.rareventure.gps2.GpsTrailerService.java

/**
 * Show a notification while this service is running.
 *//*from  www . ja v  a2 s  .c o m*/
private void showCurrentNotification() {
    //      Log.d(TAG, "Showing current notification");
    String CHANNEL_ID = "gpstrailer_channel";

    if (Build.VERSION.SDK_INT >= 26) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Tiny Travel Tracker",
                NotificationManager.IMPORTANCE_DEFAULT);

        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                .createNotificationChannel(channel);
    }

    if (currentNotificationSetting.msgId == -1 && currentNotificationSetting.iconId == -1) {
        if (Build.VERSION.SDK_INT >= 26) {
            //we still need to create an icon, even though we are shutting down
            //or android will kill our app
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Tiny Travel Tracker",
                    NotificationManager.IMPORTANCE_DEFAULT);

            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                    .createNotificationChannel(channel);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID).setContentTitle("")
                    .setContentText("").build();

            startForeground(1, notification);
        }
        return;
    }

    CharSequence text = getText(currentNotificationSetting.msgId);

    // Set the icon, scrolling text and timestamp
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
    builder.setSmallIcon(currentNotificationSetting.iconId);
    builder.setOngoing(currentNotificationSetting.isOngoing);
    builder.setAutoCancel(!currentNotificationSetting.isOngoing);
    builder.setContentText(text);
    builder.setContentTitle("Tiny Travel Tracker");

    // The PendingIntent to launch our activity if the user selects this notification
    // TODO 2.5 make settings lite for notification bar only. Set it's task affinity
    // different from the main app so hitting back doesn't cause "enter password" to be asked
    Intent intent = new Intent(this, SettingsActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            currentNotificationSetting.intent != null ? currentNotificationSetting.intent : intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(contentIntent);

    Notification notification = builder.build();

    startForeground(1, notification);

}

From source file:github.popeen.dsub.util.Notifications.java

@TargetApi(Build.VERSION_CODES.O)
private static NotificationChannel getDownloadingNotificationChannel(Context context) {
    if (downloadingChannel == null) {
        downloadingChannel = new NotificationChannel("downloading-channel", "Downloading Notification",
                NotificationManager.IMPORTANCE_LOW);
        downloadingChannel.setDescription("Ongoing downloading notification to keep the service alive");

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(downloadingChannel);
    }/*from  ww  w. j ava  2  s .  com*/

    return downloadingChannel;
}

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 .j a  va  2s . 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.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);//  www  . ja v a 2s  . c o 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:org.pocketworkstation.pckeyboard.LatinIME.java

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.notification_channel_name);
        String description = getString(R.string.notification_channel_description);
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }//w  w  w. j  a va  2 s  . com
}

From source file:com.z3r0byte.magistify.Services.BackgroundService.java

private void newGradeNotification() {
    if (!allowDataTransfer()) {
        return;//from   w w  w .j  a va 2s .  c om
    }
    Magister magister = GlobalAccount.MAGISTER;
    if (magister == null || magister.isExpired()) {
        Log.e(TAG, "New Grade Notification: Invalid magister");
    } else {

        GradeHandler gradeHandler = new GradeHandler(magister);
        Grade[] gradeArray;
        List<Grade> gradeList = new ArrayList<Grade>();
        try {
            gradeArray = gradeHandler.getRecentGrades();
            gradesdb.addGrades(gradeArray);
            Collections.reverse(Arrays.asList(gradeArray));

            //For testing purposes:
            /*Grade sampleGrade = new Grade();
            sampleGrade.isSufficient = false;
            sampleGrade.grade = "2.3";
            sampleGrade.subject = new SubSubject();
            sampleGrade.subject.name = "Latijn";
                    
            Grade sampleGrade2 = new Grade();
            sampleGrade2.isSufficient = true;
            sampleGrade2.grade = "6.5";
            sampleGrade2.subject = new SubSubject();
            sampleGrade2.subject.name = "Nederlands";
                    
            gradeArray = new Grade[2];
            gradeArray[0] = sampleGrade;
            gradeArray[1] = sampleGrade2;*/
            for (Grade grade : gradeArray) {
                if (!gradesdb.hasBeenSeen(grade, false)
                        && (grade.isSufficient || !configUtil.getBoolean("pass_grades_only"))) {
                    gradeList.add(grade);
                }
            }

        } catch (IOException | AssertionError | NullPointerException e) {
            e.printStackTrace();
            return;
        }
        String GradesNotification = mGson.toJson(gradeList);
        if (gradeList.size() > 0
                && !configUtil.getString("lastGradesNotification").equals(GradesNotification)) {

            Log.d(TAG, "New Grade Notification: Some grades to show: " + gradeList.size());

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
            mBuilder.setSmallIcon(R.drawable.ic_grade_notification);

            if (gradeList.size() == 1) {
                Grade grade = gradeList.get(0);
                if (grade.description != null) {
                    mBuilder.setContentTitle(
                            "Nieuw cijfer voor " + grade.subject.name + " - " + grade.description);
                } else {
                    mBuilder.setContentTitle("Nieuw cijfer voor " + grade.subject.name);
                }
                //mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText())
                mBuilder.setContentText("Een " + grade.grade);
            } else {
                CharSequence content = "";
                for (Grade grade : gradeList) {
                    CharSequence string;
                    if (grade.description != null) {
                        string = grade.subject.name + " - " + grade.description + ": "
                                + Html.fromHtml("<strong>" + grade.grade + "</strong>");
                    } else {
                        string = grade.subject.name + ", een " + grade.grade;
                    }
                    if (content.length() > 1) {
                        content = content + "\n" + string;
                    } else {
                        content = string;
                    }
                }
                mBuilder.setContentTitle("Nieuwe cijfers voor:");
                mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(content));
                mBuilder.setContentText(content);
            }
            mBuilder.setAutoCancel(true);
            mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);
            mBuilder.setLights(Color.LTGRAY, 300, 200);

            Intent resultIntent = new Intent(context, NewGradeActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(NewGradeActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);

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

            if (Build.VERSION.SDK_INT >= 26) {
                String channelId = NEW_GRADE_NOTIFICATIONCHANNEL_ID;
                CharSequence channelName = NEW_GRADE_NOTIFICATIONCHANNEL_ID;
                int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName,
                        importance);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.enableVibration(true);
                notificationChannel.setImportance(NotificationManager.IMPORTANCE_DEFAULT);
                notificationChannel.setVibrationPattern(new long[] { 100, 200, 150 });
                mNotificationManager.createNotificationChannel(notificationChannel);
                mBuilder.setChannelId(NEW_GRADE_NOTIFICATIONCHANNEL_ID);
            }

            mNotificationManager.notify(NEW_GRADE_NOTIFICATION_ID, mBuilder.build());

            configUtil.setString("lastGradesNotification", GradesNotification);
        } else {
            Log.w(TAG, "New Grade Notification: No grades!");
        }
    }
}

From source file:github.popeen.dsub.util.Notifications.java

@TargetApi(Build.VERSION_CODES.O)
private static NotificationChannel getSyncNotificationChannel(Context context) {
    if (syncChannel == null) {
        syncChannel = new NotificationChannel("sync-channel", "Sync Notifications",
                NotificationManager.IMPORTANCE_MIN);
        syncChannel.setDescription("Sync notifications");

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(syncChannel);
    }/*  ww w.ja  va 2s  .co  m*/

    return syncChannel;
}