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:kr.ds.say.MyFirebaseMessagingService.java

private void sendNotification(String message, Bitmap bitmap) {
    Intent intent = new Intent(this, IntroActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.channel_message_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setPriority(NotificationCompat.PRIORITY_MAX).setSmallIcon(R.mipmap.icon)
            .setContentTitle(getString(R.string.app_name)).setContentText(message).setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent);

    if (bitmap != null) {
        NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
        style.setBigContentTitle(getString(R.string.app_name));
        style.setSummaryText(message);/*from   w  w  w  .j av  a  2s. com*/
        style.bigPicture(bitmap);
        notificationBuilder.setStyle(style);
    }

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, getString(R.string.channel_message_id),
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(getString(R.string.channel_message));
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(UniqueID.getRandomNumber(1000), notificationBuilder.build());
}

From source file:tm.android.chronos.services.TimerRunner.java

private void createNotification(long duration) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

    String channelId = "channel-01";

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        String channelName = "Chronos Channel";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(channelId, channelName, importance);
        notificationManager.createNotificationChannel(mChannel);
    }// w w  w .  ja v a  2s  . c om

    int min = (int) (duration / 60000);
    if (Units.hasNoResources())
        Units.setResources(context.getResources());
    String text = Units.getLocalizedTextWithParams("timer_notification", min + "", (min > 1 ? "s" : ""));//"Timer of " + min + " minute" + (min > 1 ? "s" : "") + " is finished";
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle(context.getResources().getString(R.string.app_name_timer)).setContentText(text)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent).setAutoCancel(true).setCategory(NotificationCompat.CATEGORY_ALARM);

    notificationManager.notify(0, mBuilder.build());
}

From source file:chat.viska.android.Application.java

private void initializeNotificationChannels() {
    if (Build.VERSION.SDK_INT >= 26) {
        final NotificationManager manager = getSystemService(NotificationManager.class);

        final NotificationChannel systemChannel = new NotificationChannel(KEY_NOTIF_CHANNEL_SYSTEM,
                getString(R.string.title_notif_channel_system), NotificationManager.IMPORTANCE_LOW);
        systemChannel.setDescription(getString(R.string.desc_notif_channel_system));
        systemChannel.enableVibration(false);
        systemChannel.enableLights(false);
        systemChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        systemChannel.setShowBadge(false);
        manager.createNotificationChannel(systemChannel);

        final NotificationChannel messagingChannel = new NotificationChannel(KEY_NOTIF_CHANNEL_MESSAGES,
                getString(R.string.title_notif_channel_messaging), NotificationManager.IMPORTANCE_HIGH);
        systemChannel.setDescription(getString(R.string.desc_notif_channel_messaging));
        systemChannel.enableVibration(true);
        systemChannel.enableLights(true);
        systemChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        systemChannel.setShowBadge(true);
        manager.createNotificationChannel(messagingChannel);
    }/*from  ww w . j a  v  a2  s  . c o m*/
}

From source file:com.tourmaline.example.ExampleApplication.java

public void initEngine(final boolean automaticMonitoring, final CompletionListener completionListener) {

    //TLKit is a foreground service: here we set what is displayed into the
    // device notification area

    final String NOTIF_CHANNEL_ID = "background-run-notif-channel-id";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        final NotificationChannel channel = new NotificationChannel(NOTIF_CHANNEL_ID,
                getText(R.string.foreground_notification_content_text), NotificationManager.IMPORTANCE_NONE);
        channel.setShowBadge(false);//from w ww  .jav  a  2  s.com
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

    final Notification note = new NotificationCompat.Builder(this, NOTIF_CHANNEL_ID)
            .setContentTitle(getText(R.string.app_name))
            .setContentText(getText(R.string.foreground_notification_content_text))
            .setSmallIcon(R.mipmap.ic_foreground_notification).setPriority(NotificationCompat.PRIORITY_MIN)
            .build();

    String hashedUserId = HashId(user);
    Engine.Init(getApplicationContext(), ApiKey, hashedUserId, automaticMonitoring, note, completionListener);

}

From source file:org.deviceconnect.android.deviceplugin.host.recorder.AbstractPreviewServerProvider.java

/**
 * Notification??./*from   w  w  w.j ava 2s  .  c o m*/
 */
public void sendNotification() {
    PendingIntent contentIntent = createPendingIntent();
    Notification notification = createNotification(contentIntent, null);
    NotificationManager manager = (NotificationManager) mContext.getSystemService(Service.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = mContext.getResources().getString(R.string.overlay_preview_channel_id);
        NotificationChannel channel = new NotificationChannel(channelId,
                mContext.getResources().getString(R.string.overlay_preview_content_title),
                NotificationManager.IMPORTANCE_LOW);
        channel.setDescription(mContext.getResources().getString(R.string.overlay_preview_content_message));
        manager.createNotificationChannel(channel);
        notification = createNotification(contentIntent, channelId);
    }
    manager.notify(getId(), getNotificationId(), notification);
}

From source file:arun.com.chromer.webheads.WebHeadService.java

@NonNull
@Override/*from  ww  w . ja va2 s  .c  om*/
public Notification getNotification() {
    if (Utils.ANDROID_OREO) {
        final NotificationChannel channel = new NotificationChannel(WebHeadService.class.getName(),
                getString(R.string.web_heads_service), NotificationManager.IMPORTANCE_MIN);
        channel.setDescription(getString(R.string.app_detection_notification_channel_description));
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
    final PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_STOP_WEBHEAD_SERVICE), FLAG_UPDATE_CURRENT);
    final PendingIntent contextActivity = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_OPEN_CONTEXT_ACTIVITY), FLAG_UPDATE_CURRENT);
    final PendingIntent newTab = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_OPEN_NEW_TAB),
            FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, WebHeadService.class.getName())
            .setSmallIcon(R.drawable.ic_chromer_notification).setPriority(PRIORITY_MIN)
            .setContentText(getString(R.string.tap_close_all))
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .addAction(R.drawable.ic_add, getText(R.string.open_new_tab), newTab)
            .addAction(R.drawable.ic_list, getText(R.string.manage), contextActivity)
            .setContentTitle(getString(R.string.web_heads_service)).setContentIntent(contentIntent)
            .setAutoCancel(false).setLocalOnly(true).build();
    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    return notification;
}

From source file:com.frostwire.android.gui.NotificationUpdateDemon.java

private void updatePermanentStatusNotification() {
    if (!ConfigurationManager.instance()
            .getBoolean(Constants.PREF_KEY_GUI_ENABLE_PERMANENT_STATUS_NOTIFICATION)) {
        return;// w  w  w . j a  va 2 s  . co  m
    }

    if (notificationViews == null || notificationObject == null) {
        LOG.warn("Notification views or object are null, review your logic");
        return;
    }

    // number of uploads (seeding) and downloads
    TransferManager transferManager;

    try {
        transferManager = TransferManager.instance();
    } catch (IllegalStateException btEngineNotReadyException) {
        return;
    }

    if (transferManager != null) {
        int downloads = transferManager.getActiveDownloads();
        int uploads = transferManager.getActiveUploads();
        if (downloads == 0 && uploads == 0) {
            NotificationManager manager = (NotificationManager) mParentContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                try {
                    manager.cancel(Constants.NOTIFICATION_FROSTWIRE_STATUS);
                } catch (SecurityException ignored) {
                    // possible java.lang.SecurityException
                }
            }
            return; // quick return
        }
        //  format strings
        String sDown = UIUtils.rate2speed(transferManager.getDownloadsBandwidth() / 1024);
        String sUp = UIUtils.rate2speed(transferManager.getUploadsBandwidth() / 1024);
        // Transfers status.
        notificationViews.setTextViewText(R.id.view_permanent_status_text_downloads, downloads + " @ " + sDown);
        notificationViews.setTextViewText(R.id.view_permanent_status_text_uploads, uploads + " @ " + sUp);
        final NotificationManager notificationManager = (NotificationManager) mParentContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            try {
                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);
                    notificationManager.createNotificationChannel(channel);
                }
                notificationManager.notify(Constants.NOTIFICATION_FROSTWIRE_STATUS, notificationObject);
            } catch (SecurityException ignored) {
                // possible java.lang.SecurityException
                ignored.printStackTrace();
            } catch (Throwable ignored2) {
                // possible android.os.TransactionTooLargeException
                ignored2.printStackTrace();
            }
        }
    }
}

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: {/* w w  w .ja  v a  2s.c  om*/
        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:org.mozilla.focus.session.SessionNotificationService.java

public void createNotificationChannelIfNeeded() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        // Notification channels are only available on Android O or higher.
        return;/*from w  ww .j  av  a2 s . c  om*/
    }

    final NotificationManager notificationManager = getSystemService(NotificationManager.class);
    if (notificationManager == null) {
        return;
    }

    final String notificationChannelName = getString(R.string.notification_browsing_session_channel_name);
    final String notificationChannelDescription = getString(
            R.string.notification_browsing_session_channel_description, getString(R.string.app_name));

    final NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
            notificationChannelName, NotificationManager.IMPORTANCE_MIN);
    channel.setImportance(NotificationManager.IMPORTANCE_LOW);
    channel.setDescription(notificationChannelDescription);
    channel.enableLights(false);
    channel.enableVibration(false);
    channel.setShowBadge(true);

    notificationManager.createNotificationChannel(channel);
}

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

private void appointmentNotification() {
    Appointment[] appointments = calendarDB.getNotificationAppointments();
    Log.d(TAG, "AppointmentNotifications: amount of appointments that should be shown: " + appointments.length);
    String previousAppointment = configUtil.getString("previous_appointment");
    if (appointments.length >= 1) {
        Appointment appointment = appointments[0];
        if (!mGson.toJson(appointment).equals(previousAppointment) && isCandidate(appointment)) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
            mBuilder.setSmallIcon(R.drawable.ic_appointment);

            if (appointment.startDate != null) {
                String time = DateUtils.formatDate(appointment.startDate, "HH:mm");
                mBuilder.setContentTitle("Volgende afspraak (" + time + ")");
            } else {
                mBuilder.setContentTitle("Volgende afspraak:");
            }//from w  w  w . ja  va 2 s . c o  m
            mBuilder.setContentText(appointment.description + " in " + appointment.location);
            mBuilder.setAutoCancel(true);
            mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

            Intent resultIntent = new Intent(context, AppointmentActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(AppointmentActivity.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 = APPOINTMENT_NOTIFICATIONCHANNEL_ID;
                CharSequence channelName = APPOINTMENT_NOTIFICATIONCHANNEL_ID;
                int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName,
                        importance);
                notificationChannel.enableLights(false);
                notificationChannel.enableVibration(false);
                notificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
                mNotificationManager.createNotificationChannel(notificationChannel);
                mBuilder.setChannelId(APPOINTMENT_NOTIFICATIONCHANNEL_ID);
            }
            mNotificationManager.notify(APPOINTMENT_NOTIFICATION_ID, mBuilder.build());

            previousAppointment = mGson.toJson(appointment);
            configUtil.setString("previous_appointment", previousAppointment);
        }
    } else {
        NotificationManager notifManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notifManager.cancel(APPOINTMENT_NOTIFICATION_ID);
    }
}