Example usage for android.app NotificationManager createNotificationChannel

List of usage examples for android.app NotificationManager createNotificationChannel

Introduction

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

Prototype

public void createNotificationChannel(@NonNull NotificationChannel channel) 

Source Link

Document

Creates a notification channel that notifications can be posted to.

Usage

From source file:com.frostwire.android.gui.services.EngineService.java

public void notifyDownloadFinished(String displayName, File file, String infoHash) {
    try {//from w  ww. jav a 2  s.  c  o  m
        if (notifiedStorage.contains(infoHash)) {
            // already notified
            return;
        } else {
            notifiedStorage.add(infoHash);
        }

        Context context = getApplicationContext();
        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        i.putExtra(Constants.EXTRA_DOWNLOAD_COMPLETE_NOTIFICATION, true);
        i.putExtra(Constants.EXTRA_DOWNLOAD_COMPLETE_PATH, file.getAbsolutePath());
        PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(context,
                Constants.FROSTWIRE_NOTIFICATION_CHANNEL_ID).setWhen(System.currentTimeMillis())
                        .setContentText(getString(R.string.download_finished))
                        .setContentTitle(getString(R.string.download_finished))
                        .setSmallIcon(getNotificationIcon()).setContentIntent(pi).build();
        notification.vibrate = ConfigurationManager.instance().vibrateOnFinishedDownload() ? VENEZUELAN_VIBE
                : null;
        notification.number = TransferManager.instance().getDownloadsToReview();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        if (manager != null) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        Constants.FROSTWIRE_NOTIFICATION_CHANNEL_ID, "FrostWire",
                        NotificationManager.IMPORTANCE_MIN);
                channel.setSound(null, null);
                manager.createNotificationChannel(channel);
            }
            manager.notify(Constants.NOTIFICATION_DOWNLOAD_TRANSFER_FINISHED, notification);
        }
    } catch (Throwable e) {
        LOG.error("Error creating notification for download finished", e);
    }
}

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

private void newHomeworkNotification() {
    Appointment[] newhomework = homeworkDB.getAppointmentsWithHomework();
    ArrayList<Appointment> homeworkList = new ArrayList<>();
    for (Appointment appointment : newhomework) {
        if (homeworkDB.isModified(appointment, true))
            homeworkList.add(appointment);
    }// w  w  w .  j ava 2  s  . co m
    Log.d(TAG, "newHomeworkNotification: Amount of new homework: " + homeworkList.size());
    if (homeworkList.size() > 0) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.ic_new_homework);

        String content = "Je hebt nieuw of aangepast huiswerk voor:";
        for (Appointment appointment : homeworkList) {
            content = content + "\n" + appointment.description
                    + DateUtils.formatDate(appointment.startDate, "' ('dd MMM')'");
        }

        mBuilder.setContentTitle("Nieuw huiswerk");
        mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(content));
        mBuilder.setContentText(content);
        mBuilder.setAutoCancel(true);
        mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);

        Intent resultIntent = new Intent(context, HomeworkActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(ScheduleChangeActivity.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_HOMEWORK_NOTIFICATIONCHANNEL_ID;
            CharSequence channelName = NEW_HOMEWORK_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_HOMEWORK_NOTIFICATIONCHANNEL_ID);
        }
        mNotificationManager.notify(NEW_HOMEWORK_NOTIFICATION_ID, mBuilder.build());
    }
}

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

private void nextAppointmentChangedNotification() {
    Appointment[] appointments = scheduleChangeDB.getNotificationAppointments();
    String previousChangedAppointment = configUtil.getString("previous_changed_appointment");
    if (appointments.length > 0) {
        Appointment appointment = appointments[0];
        if (!appointment.startDateString.equals(previousChangedAppointment)) {
            String content;/* www . j  av a  2 s.co  m*/
            if (appointment.description != null && !appointment.description.equalsIgnoreCase("null")) {
                content = appointment.description;
            } else {
                content = "De les is uitgevallen!";
            }

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

            mBuilder.setContentTitle("Let op! De volgende les is gewijzigd!");
            mBuilder.setContentText(content);
            mBuilder.setAutoCancel(true);
            mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);

            Intent resultIntent = new Intent(context, ScheduleChangeActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(ScheduleChangeActivity.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 = NEXT_APPOINTMENT_CHANGED_NOTIFICATIONCHANNEL_ID;
                CharSequence channelName = NEXT_APPOINTMENT_CHANGED_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(NEXT_APPOINTMENT_CHANGED_NOTIFICATIONCHANNEL_ID);
            }
            mNotificationManager.notify(NEXT_APPOINTMENT_CHANGED_NOTIFICATION_ID, mBuilder.build());

            configUtil.setString("previous_changed_appointment", appointment.startDateString);
        }
    } else {
        NotificationManager notifManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notifManager.cancel(NEXT_APPOINTMENT_CHANGED_NOTIFICATION_ID);
    }
}

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

/**
 * Add a notification groups./* w w w.j  av  a2  s  .  co 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.z3r0byte.magistify.Services.BackgroundService.java

private void manageSession(final User user, final School school) {
    if (allowDataTransfer()) {
        if (GlobalAccount.MAGISTER == null) {
            if (configUtil.getInteger("failed_auth") >= 2) {
                Log.w(TAG, "run: Warning! 2 Failed authentications, aborting for user's safety!");
                if (configUtil.getBoolean("shown_failed_login_notification"))
                    return;
                configUtil.setBoolean("shown_failed_login_notification", true);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
                mBuilder.setSmallIcon(R.drawable.ic_error);

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

                mBuilder.setContentIntent(resultPendingIntent);
                mBuilder.setContentTitle(context.getString(R.string.dialog_login_failed_title));
                mBuilder.setContentText(context.getString(R.string.msg_fix_login));
                mBuilder.setAutoCancel(true);
                mBuilder.setSound(null);
                mBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);

                NotificationManager mNotificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                if (Build.VERSION.SDK_INT >= 26) {
                    String channelId = LOGIN_FAILED_CHANNEL_ID;
                    CharSequence channelName = LOGIN_FAILED_CHANNEL_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(LOGIN_FAILED_CHANNEL_ID);
                }/*from  w ww .  ja  v  a 2  s  .  c om*/
                mNotificationManager.notify(LOGIN_FAILED_ID, mBuilder.build());
            } else {
                configUtil.setBoolean("shown_failed_login_notification", false);
                try {
                    Log.d(TAG, "SessionManager: initiating session");
                    GlobalAccount.MAGISTER = Magister.login(school, user.username, user.password);
                    configUtil.setInteger("failed_auth", 0);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (InvalidParameterException e) {
                    int fails = configUtil.getInteger("failed_auth");
                    fails++;
                    configUtil.setInteger("failed_auth", fails);
                    Log.w(TAG, "SessionManager: Amount of failed Authentications: " + fails);
                }
            }
        } else if (GlobalAccount.MAGISTER.isExpired()) {
            configUtil.setBoolean("shown_failed_login_notification", false);
            try {
                GlobalAccount.MAGISTER.login();
                Log.d(TAG, "SessionManager: refreshing session");
                configUtil.setInteger("failed_auth", 0);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidParameterException e) {
                e.printStackTrace();
                int fails = configUtil.getInteger("failed_auth");
                fails++;
                configUtil.setInteger("failed_auth", fails);
                Log.w(TAG, "SessionManager: Amount of failed Authentications: " + fails);
            }
        } else {
            Log.d(TAG, "manageSession: Session still valid");
        }
    }
}

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

private void newGradeNotification() {
    if (!allowDataTransfer()) {
        return;/*from www. j  av a2s  .c  o  m*/
    }
    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:com.z3r0byte.magistify.Services.BackgroundService.java

private void newScheduleChangeNotification() {
    if (allowDataTransfer()) {
        Magister magister = GlobalAccount.MAGISTER;
        if (magister == null || magister.isExpired()) {
            Log.d(TAG, "ScheduleChangeNotification: No valid Magister");
        } else {/*w w  w  . j  av  a2s .  c om*/

            AppointmentHandler appointmentHandler = new AppointmentHandler(magister);
            Appointment[] appointments;
            try {
                Log.d(TAG, "ScheduleChangeNotification: Requesting schedule changes....");
                appointments = appointmentHandler.getScheduleChanges(DateUtils.getToday(),
                        DateUtils.addDays(DateUtils.getToday(), 3));
            } catch (IOException | AssertionError e) {
                Log.d(TAG, "ScheduleChangeNotification: Error while requesting schedule changes");
                e.printStackTrace();
                return;
            }

            Boolean newChanges = false;
            if (appointments == null || appointments.length < 1) {
                return;
            } else {
                Log.d(TAG, "ScheduleChangeNotification: Checking for new changes....");
                for (Appointment appointment : appointments) {
                    if (!scheduleChangeDB.isInDatabase(appointment)) {
                        newChanges = true;
                    }
                }
                scheduleChangeDB.addItems(appointments);
            }

            if (newChanges) {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
                mBuilder.setSmallIcon(R.drawable.ic_schedule_change);

                mBuilder.setContentTitle("Nieuwe roosterijziging(en)!");
                mBuilder.setContentText("Tik om te bekijken");
                mBuilder.setAutoCancel(true);
                mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                mBuilder.setDefaults(Notification.DEFAULT_ALL);

                Intent resultIntent = new Intent(context, ScheduleChangeActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(ScheduleChangeActivity.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_SCHEDULE_CHANGE_NOTIFICATIONCHANNEL_ID;
                    CharSequence channelName = NEW_SCHEDULE_CHANGE_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_SCHEDULE_CHANGE_NOTIFICATIONCHANNEL_ID);
                }
                mNotificationManager.notify(NEW_SCHEDULE_CHANGE_NOTIFICATION_ID, mBuilder.build());
            }
        }

    }
}

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

private void unFinishedHomeworkNotification() {
    String lastCheck = configUtil.getString("last_unfinished_homework_check");
    if (lastCheck.equals(""))
        lastCheck = DateUtils.formatDate(DateUtils.addDays(new Date(), -1), "yyyyMMdd");

    Date lastCheckDate = DateUtils.parseDate(lastCheck, "yyyyMMdd");
    Date now = DateUtils.parseDate(DateUtils.formatDate(new Date(), "yyyyMMdd"), "yyyyMMdd");
    Integer hours = configUtil.getInteger("unfinished_homework_hour");
    Integer minutes = configUtil.getInteger("unfinished_homework_minute");
    if (lastCheckDate.before(now)) {
        lastCheckDate = DateUtils.addHours(lastCheckDate, 24 + hours);
        lastCheckDate = DateUtils.addMinutes(lastCheckDate, minutes);
        if (new Date().after(lastCheckDate)) {
            Appointment[] appointments = calendarDB.getUnfinishedAppointments(DateUtils.addDays(now, 1));

            if (appointments.length > 0) {

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
                mBuilder.setSmallIcon(R.drawable.ic_unfinished_homework);
                mBuilder.setContentTitle("Huiswerk waarschuwing");

                if (appointments.length == 1) {
                    mBuilder.setContentText(appointments[0].description);
                } else {
                    String content = "Je hebt je huiswerk voor de volgende lessen van morgen nog niet afgerond:";
                    for (Appointment appointment : appointments) {
                        String string = appointment.description;
                        if (content.length() > 1) {
                            content = content + "\n" + string;
                        } else {
                            content = string;
                        }//from w  ww  .jav a2s.c o m
                    }
                    mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(content));
                    mBuilder.setContentText(content);
                }
                mBuilder.setAutoCancel(true);
                mBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
                mBuilder.setDefaults(Notification.DEFAULT_ALL);
                mBuilder.setLights(Color.RED, 300, 200);

                Intent resultIntent = new Intent(context, HomeworkActivity.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 = UNFINISHED_HOMEWORK_NOTIFICATIONCHANNEL_ID;
                    CharSequence channelName = UNFINISHED_HOMEWORK_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(UNFINISHED_HOMEWORK_NOTIFICATIONCHANNEL_ID);
                }
                mNotificationManager.notify(UNFINISHED_HOMEWORK_NOTIFICATION_ID, mBuilder.build());
            }

            configUtil.setString("last_unfinished_homework_check",
                    DateUtils.formatDate(new Date(), "yyyyMMdd"));
        }
    }
}

From source file:org.restcomm.android.sdk.RCDevice.java

/**
 * Method returns the Notification builder
 * For Oreo devices we can have channels with HIGH and LOW importance.
 * If highImportance is true builder will be created with HIGH priority
 * For pre Oreo devices builder without channel will be returned
 * @param highImportance true if we need HIGH channel, false if we need LOW
 * @return//  w w  w .j a  v  a  2s.c  om
 */
private NotificationCompat.Builder getNotificationBuilder(boolean highImportance) {
    NotificationCompat.Builder builder;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        if (highImportance) {
            NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL_ID, PRIMARY_CHANNEL,
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setLightColor(Color.GREEN);
            channel.enableLights(true);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            channel.enableVibration(true);
            channel.setVibrationPattern(notificationVibrationPattern);

            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                    .createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(RCDevice.this, PRIMARY_CHANNEL_ID);
        } else {
            NotificationManager notificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = new NotificationChannel(DEFAULT_FOREGROUND_CHANNEL_ID,
                    DEFAULT_FOREGROUND_CHANNEL, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(notificationChannel);

            builder = new NotificationCompat.Builder(RCDevice.this, DEFAULT_FOREGROUND_CHANNEL_ID);
        }

    } else {
        builder = new NotificationCompat.Builder(RCDevice.this);
    }

    return builder;
}

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 w w .j  a 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;
}