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

private void sendNotification(String message) {
    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);

    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);
    }//  ww  w . ja  v a2s  .c  om

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

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;//  w w  w  .  java 2s . c  o  m
    }

    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.frostwire.android.gui.NotificationUpdateDemon.java

private void updatePermanentStatusNotification() {
    if (!ConfigurationManager.instance()
            .getBoolean(Constants.PREF_KEY_GUI_ENABLE_PERMANENT_STATUS_NOTIFICATION)) {
        return;//  ww w.j  a  v  a  2 s.com
    }

    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.instify.android.services.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */// w ww .  j ava  2s.c  om
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.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.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_notification_white).setContentTitle("FCM Message")
            .setContentText(messageBody).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    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, "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:cl.chihau.holaauto.MyMessagingService.java

public void mostrarNotificacion(int id, Notification notificacion) {
    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String name = "my channel";
        String description = "channel description";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(canal, name, importance);
        channel.setDescription(description);

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

        manager.createNotificationChannel(channel);
    }/*ww w . ja va  2 s  . co  m*/

    mNotificationManager.notify(id, notificacion);

}

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

/**
 * Add a notification groups.//w ww  .  j a  va  2 s . c o  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.peptrack.gps.locationupdatesforegroundservice.LocationUpdatesService.java

@Override
public void onCreate() {
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mLocationCallback = new LocationCallback() {
        @Override//  ww  w .j  a v  a  2  s  .  c o m
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            onNewLocation(locationResult.getLastLocation());
        }
    };

    createLocationRequest();
    getLastLocation();

    HandlerThread handlerThread = new HandlerThread(TAG);
    handlerThread.start();
    mServiceHandler = new Handler(handlerThread.getLooper());
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Android O requires a Notification Channel.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.app_name);
        // Create the channel for the notification
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,
                NotificationManager.IMPORTANCE_DEFAULT);

        // Set the Notification Channel for the Notification Manager.
        mNotificationManager.createNotificationChannel(mChannel);
    }
}

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

@NonNull
@Override//  w ww .j a  va2 s  .c o m
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.shoppingspree.shoppingspree.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *///from w  ww . j a v a  2  s.  co m
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.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.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle("Buy Through Shopping Spree")
            .setContentText(messageBody).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    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, "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:com.google.firebase.quickstart.fcm.java.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*w  ww  .  jav  a2s . com*/
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.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.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle(getString(R.string.fcm_message))
            .setContentText(messageBody).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    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, "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}