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.cryart.sabbathschool.misc.SSReminderService.java

@Override
public boolean onStartJob(JobParameters job) {
    Context context = getBaseContext();
    SSReminder.scheduleAlarms(context);/*  w  w w  .  ja  va  2 s  .  c  o m*/
    try {
        String channelId = "ss_notification_channel";
        String channelName = getString(R.string.app_name);

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = new NotificationChannel(channelId, channelName, importance);
            _SSNotificationManager.createNotificationChannel(mChannel);
        }
        Intent _SSContentIntent = new Intent(context, SSSplashActivity.class);

        Intent _SSShareIntent = new Intent();

        _SSContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _SSShareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _SSShareIntent.setAction(Intent.ACTION_SEND);
        _SSShareIntent.putExtra(Intent.EXTRA_TEXT, "");

        _SSShareIntent.setType("*/*");

        PendingIntent _SSPendingContentIntent = PendingIntent.getActivity(context, 0, _SSContentIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent _SSPendingShareIntent = PendingIntent.getActivity(context, 0,
                Intent.createChooser(_SSShareIntent, context.getString(R.string.ss_share)),
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder _SSNotificationBuilder = new NotificationCompat.Builder(context,
                "ss_notification_channel").setSmallIcon(R.mipmap.ic_stat_notification)
                        .setContentTitle(context.getString(R.string.ss_app_name))
                        .setColor(Color.parseColor(SSColorTheme.getInstance().getColorPrimary()))
                        .addAction(0, context.getString(R.string.ss_menu_read_now), _SSPendingContentIntent)
                        .addAction(0, context.getString(R.string.ss_share), _SSPendingShareIntent)
                        .setAutoCancel(true).setVibrate(new long[] { 1000, 1000 })
                        .setContentIntent(_SSPendingContentIntent)
                        .setContentText(context.getString(R.string.ss_settings_reminder_text));

        _SSNotificationManager.notify(1, _SSNotificationBuilder.build());
    } catch (Exception e) {
        Crashlytics.log(e.getMessage());
    }

    return false; // Answers the question: "Is there still work going on?"
}

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.
 *//*from  w w  w.ja  va2  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_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:com.shoppingspree.shoppingspree.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*from   www. j av  a  2  s.  c  o  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:org.LK8000.MyService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    /* add an icon to the notification area while LK8000 runs, to
    remind the user that we're sucking his battery empty */

    final String CHANNEL_ID = getApplicationContext().getPackageName() + "_NotificationChannel";

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Support for Android Oreo: Notification Channels
        NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
        if (channel == null) {
            channel = new NotificationChannel(CHANNEL_ID, "LK8000", NotificationManager.IMPORTANCE_LOW);

            manager.createNotificationChannel(channel);
        }/*from   w w  w .  j  av a 2  s  . c om*/
    }

    Intent intent2 = new Intent(this, mainActivityClass);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent2, 0);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID);
    notification.setSmallIcon(R.drawable.notification_icon);
    notification.setLargeIcon(BitmapFactory.decodeResource(getResources(), getApplicationInfo().icon));
    notification.setContentTitle("LK8000 is running");
    notification.setContentText("Touch to open");
    notification.setContentIntent(contentIntent);
    notification.setWhen(System.currentTimeMillis());
    notification.setShowWhen(false);
    notification.setOngoing(true);
    notification.setOnlyAlertOnce(true);

    startForeground(1, notification.build());

    /* We want this service to continue running until it is explicitly
       stopped, so return sticky */
    return START_STICKY;
}

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.
 *///  ww w  .j a  v  a2s .  c o  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(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());
}

From source file:sachinjain024.practicebook.io.firebase_android_notifications.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param notification FCM notification received.
 */// www .ja v a2 s .  c o  m
private void sendNotification(RemoteMessage.Notification notification) {
    String title = notification.getTitle();
    String messageBody = notification.getBody();
    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_launcher_foreground)
            .setBadgeIconType(R.drawable.common_google_signin_btn_icon_light)
            .setColor(getResources().getColor(R.color.colorAccent)).setContentTitle(title + "Title Updated")
            .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:kr.ds.mymunsang.MyFirebaseMessagingService.java

private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity2.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.drawable.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);
    }/* w ww.  j a  va  2  s. c o  m*/

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

From source file:de.linuxwhatelse.android.notify.activities.MainActivity.java

private void initChannels(Context context, String channelName) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        return;/*from   w ww. ja v a2s  .c  o m*/
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel(Notify.NOTIFICATION_CHANNEL, channelName,
            NotificationManager.IMPORTANCE_LOW);
    notificationManager.createNotificationChannel(channel);
}

From source file:kr.ds.mymunsang.MyFirebaseMessagingService.java

private void sendNotification(String message, Bitmap bitmap) {
    Intent intent = new Intent(this, MainActivity2.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.drawable.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);/*  ww w  .  j  a v  a2s.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(0, notificationBuilder.build());
}

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);
    }//from  w w  w  .  j  av a2s  . c  o m

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