Example usage for android.app NotificationChannel NotificationChannel

List of usage examples for android.app NotificationChannel NotificationChannel

Introduction

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

Prototype

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

Source Link

Document

Creates a notification channel.

Usage

From source file:com.readystatesoftware.chuck.internal.support.NotificationHelper.java

public NotificationHelper(Context context) {
    this.context = context;
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationManager.createNotificationChannel(new NotificationChannel(CHANNEL_ID,
                context.getString(R.string.notification_category), NotificationManager.IMPORTANCE_LOW));
        try {//from  w ww  . j a  v a  2s .c om
            setChannelId = NotificationCompat.Builder.class.getMethod("setChannelId", String.class);
        } catch (Exception ignored) {
        }
    }
}

From source file:com.sxt.chat.utils.NotificationHelper.java

/**
 * ?// w w  w .j a va  2 s  .co  m
 * <p>
 * note : notify() ? ???,?? so, Create?
 */
public NotificationHelper(Context ctx) {
    super(ctx);
    context = ctx;
    soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notify_message);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AudioAttributes att = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).build();
        NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL, "Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(soundUri, att);
        channel.setLightColor(Color.YELLOW);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(channel);

        NotificationChannel chanCustom = new NotificationChannel(CUSTOM_NOTIFY_CHANNEL, "Custom Layout Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(soundUri, att);
        chanCustom.setLightColor(Color.RED);
        chanCustom.setShowBadge(true);
        chanCustom.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(chanCustom);
    }
}

From source file:androidx.navigation.testapp.AndroidFragment.java

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    TextView tv = view.findViewById(R.id.text);
    tv.setText(getArguments().getString("myarg"));

    Button b = view.findViewById(R.id.send_notification);
    b.setOnClickListener(new View.OnClickListener() {
        @Override//from w  w w  .  jav a  2 s . co  m
        public void onClick(View v) {
            EditText editArgs = view.findViewById(R.id.edit_args);
            Bundle args = new Bundle();
            args.putString("myarg", editArgs.getText().toString());
            PendingIntent deeplink = Navigation.findNavController(v).createDeepLink()
                    .setDestination(R.id.android).setArguments(args).createPendingIntent();
            NotificationManager notificationManager = (NotificationManager) requireContext()
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationManager.createNotificationChannel(
                        new NotificationChannel("deeplink", "Deep Links", NotificationManager.IMPORTANCE_HIGH));
            }
            NotificationCompat.Builder builder = new NotificationCompat.Builder(requireContext(), "deeplink")
                    .setContentTitle("Navigation").setContentText("Deep link to Android")
                    .setSmallIcon(R.drawable.ic_android).setContentIntent(deeplink).setAutoCancel(true);
            notificationManager.notify(0, builder.build());
        }
    });
}

From source file:com.none.tom.simplerssreader.service.FeedUpdateBackgroundService.java

@SuppressWarnings({ "ConstantConditions", "deprecation" })
private static void showNotification(final Context context, final List<String> payload) {
    final NotificationManager manager = context.getSystemService(NotificationManager.class);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setBypassDnd(false);//from w  ww  .j a v  a 2  s.c o m
        channel.enableLights(true);
        channel.setShowBadge(true);
        channel.enableVibration(true);

        manager.createNotificationChannel(channel);
    }

    final PendingIntent intent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
            PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
            .setBigContentTitle(context.getString(R.string.notification_title_big));

    final int size = payload.size();

    for (int i = 0; i < size; i++) {
        style.addLine(payload.get(i));
    }

    manager.notify(ID_NOTIFICATION, new NotificationCompat.Builder(context)
            .setChannelId(NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_rss_feed_white_24dp)
            .setContentTitle(context.getString(R.string.notification_title))
            .setContentText(context.getString(R.string.notification_text)).setContentIntent(intent)
            .setStyle(style).setWhen(System.currentTimeMillis()).setAutoCancel(true).setShowWhen(true).build());
}

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: {//from   ww w.ja va2 s . c o  m
        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:ti.modules.titanium.android.notificationmanager.NotificationManagerModule.java

@SuppressLint("NewApi")
public static boolean useDefaultChannel() {
    // use default channel if we are targeting API 26+
    boolean useDefaultChannel = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
            && TiApplication.getInstance().getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O;

    // setup default channel it it does not exist
    if (useDefaultChannel && defaultChannel == null) {
        defaultChannel = new NotificationChannel(DEFAULT_CHANNEL_ID, "miscellaneous",
                NotificationManager.IMPORTANCE_DEFAULT);
        getManager().createNotificationChannel(defaultChannel);
        Log.w(TAG,/*from ww w . ja va  2 s .c  om*/
                "Falling back to default notification channel.\nIt is highly advised to create your own notification channel using Ti.Android.NotificationManager.createNotificationChannel()");
    }

    return useDefaultChannel;
}

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);
        }// ww  w  . j  a  va2s  . co m
    }

    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:org.iota.wallet.helper.NotificationHelper.java

@RequiresApi(api = Build.VERSION_CODES.O)
private static void createNotificationChannel(Context context) {
    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
    NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL,
            context.getResources().getString(R.string.app_name), NotificationManager.IMPORTANCE_MIN);
    notificationManager.createNotificationChannel(channel);
}

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);
    }/*from  ww w.  j  a  v  a  2  s .  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:de.aw.awlib.AWNotification.java

/**
 * @param context      Context//from w w w.ja va 2s.  com
 * @param contentTitle 1. Zeile der Notification
 */
public AWNotification(@NonNull Context context, @NonNull String contentTitle) {
    this.context = context;
    this.contentTitle = contentTitle;
    mNotifyID = lastNotifyID;
    lastNotifyID++;
    mChannelID = ((AWApplication) context.getApplicationContext()).getNotficationChannelID();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mNotificationChannel = new NotificationChannel(mChannelID, "Nachricht",
                NotificationManager.IMPORTANCE_DEFAULT);
    } else {
        mNotificationChannel = null;
    }
}