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.giovanniterlingen.windesheim.controllers.NotificationController.java

public void initNotificationChannels() {
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        NotificationChannel pushChannel = new NotificationChannel(PUSH_NOTIFICATION_CHANNEL,
                ApplicationLoader.applicationContext.getResources().getString(R.string.push_notification),
                NotificationManager.IMPORTANCE_HIGH);

        pushChannel.setDescription(ApplicationLoader.applicationContext.getResources()
                .getString(R.string.push_notification_description));
        pushChannel.enableLights(true);/*from  w w w.  ja  v a 2 s .c  o m*/
        pushChannel.enableVibration(true);
        pushChannel.setShowBadge(true);
        pushChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationChannel persistentChannel = new NotificationChannel(PERSISTENT_NOTIFICATION_CHANNEL,
                ApplicationLoader.applicationContext.getResources().getString(R.string.persistent_notification),
                NotificationManager.IMPORTANCE_MIN);

        persistentChannel.setDescription(ApplicationLoader.applicationContext.getResources()
                .getString(R.string.persistent_notification_description));
        persistentChannel.enableLights(false);
        persistentChannel.enableVibration(false);
        persistentChannel.setShowBadge(false);
        persistentChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        NotificationChannel serviceChannel = new NotificationChannel(SERVICE_NOTIFICATION_CHANNEL,
                ApplicationLoader.applicationContext.getResources().getString(R.string.service_notification),
                NotificationManager.IMPORTANCE_MIN);

        serviceChannel.setDescription(ApplicationLoader.applicationContext.getResources()
                .getString(R.string.service_notification_description));
        serviceChannel.enableLights(false);
        serviceChannel.enableVibration(false);
        serviceChannel.setShowBadge(false);
        serviceChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);

        NotificationManager mManager = (NotificationManager) ApplicationLoader.applicationContext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        mManager.createNotificationChannel(pushChannel);
        mManager.createNotificationChannel(persistentChannel);
        mManager.createNotificationChannel(serviceChannel);
    }
}

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.
 *//*from   w w  w.  j  a  v  a2  s. co  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:com.keylesspalace.tusky.util.NotificationManager.java

public static void createNotificationChannels(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        android.app.NotificationManager mNotificationManager = (android.app.NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        String[] channelIds = new String[] { CHANNEL_MENTION, CHANNEL_FOLLOW, CHANNEL_BOOST,
                CHANNEL_FAVOURITE };/* ww w.  j a  va 2 s .c  o  m*/
        int[] channelNames = { R.string.notification_channel_mention_name,
                R.string.notification_channel_follow_name, R.string.notification_channel_boost_name,
                R.string.notification_channel_favourite_name };
        int[] channelDescriptions = { R.string.notification_channel_mention_descriptions,
                R.string.notification_channel_follow_description,
                R.string.notification_channel_boost_description,
                R.string.notification_channel_favourite_description };

        List<NotificationChannel> channels = new ArrayList<>(4);

        for (int i = 0; i < channelIds.length; i++) {
            String id = channelIds[i];
            String name = context.getString(channelNames[i]);
            String description = context.getString(channelDescriptions[i]);
            int importance = android.app.NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(id, name, importance);

            channel.setDescription(description);
            channel.enableLights(true);
            channel.enableVibration(true);
            channel.setShowBadge(true);
            channels.add(channel);
        }

        //noinspection ConstantConditions
        mNotificationManager.createNotificationChannels(channels);

    }
}

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;/*  w  ww  . java  2 s  .c  om*/
    }

    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:com.frostwire.android.gui.services.EngineService.java

public void notifyDownloadFinished(String displayName, File file, String infoHash) {
    try {// w w w .  j a  v a  2  s. c  om
        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 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 w  w . j  a v a  2 s .c o  m*/
                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.owncloud.android.media.MediaService.java

/**
 * Initialize a service instance//from w  ww . ja va2  s. com
 * 
 * {@inheritDoc}
 */
@Override
public void onCreate() {
    super.onCreate();
    Log_OC.d(TAG, "Creating ownCloud media service");

    mWifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, MEDIA_WIFI_LOCK_TAG);

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Configure notification channel
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mNotificationChannel;
        // The user-visible name of the channel.
        CharSequence name = getString(R.string.media_service_notification_channel_name);
        // The user-visible description of the channel.
        String description = getString(R.string.media_service_notification_channel_description);
        // Set importance low: show the notification everywhere but with no sound
        int importance = NotificationManager.IMPORTANCE_LOW;
        mNotificationChannel = new NotificationChannel(MEDIA_SERVICE_NOTIFICATION_CHANNEL_ID, name, importance);
        // Configure the notification channel.
        mNotificationChannel.setDescription(description);
        mNotificationManager.createNotificationChannel(mNotificationChannel);
    }

    mNotificationBuilder = new NotificationCompat.Builder(this);
    mNotificationBuilder.setColor(this.getResources().getColor(R.color.primary));
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    mBinder = new MediaServiceBinder(this);

    // add AccountsUpdatedListener
    mAccountManager = AccountManager.get(this);
    mAccountManager.addOnAccountsUpdatedListener(new OnAccountsUpdateListener() {
        @Override
        public void onAccountsUpdated(Account[] accounts) {
            // stop playback if account of the played media files was removed
            if (mAccount != null && !AccountUtils.exists(mAccount.name, MediaService.this)) {
                processStopRequest(false);
            }
        }
    }, null, false);
}

From source file:nu.yona.app.utils.AppUtils.java

@TargetApi(Build.VERSION_CODES.O)
public static void createPersistentNotificationChannel(Context context) {
    removeOldPersistentNotificationFromChannel(context);
    NotificationChannel channel = new NotificationChannel(AppConstant.YONA_SERVICE_CHANNEL_ID,
            context.getString(R.string.yona_service_notification_channel_name),
            NotificationManager.IMPORTANCE_MIN);
    channel.setShowBadge(false);/*from   w  ww. j  a  v a2 s.c o  m*/
    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
            .createNotificationChannel(channel);
}

From source file:com.keylesspalace.tusky.util.NotificationHelper.java

public static void createNotificationChannelsForAccount(@NonNull AccountEntity account,
        @NonNull Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

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

        String[] channelIds = new String[] { CHANNEL_MENTION + account.getIdentifier(),
                CHANNEL_FOLLOW + account.getIdentifier(), CHANNEL_BOOST + account.getIdentifier(),
                CHANNEL_FAVOURITE + account.getIdentifier() };
        int[] channelNames = { R.string.notification_channel_mention_name,
                R.string.notification_channel_follow_name, R.string.notification_channel_boost_name,
                R.string.notification_channel_favourite_name };
        int[] channelDescriptions = { R.string.notification_channel_mention_descriptions,
                R.string.notification_channel_follow_description,
                R.string.notification_channel_boost_description,
                R.string.notification_channel_favourite_description };

        List<NotificationChannel> channels = new ArrayList<>(4);

        NotificationChannelGroup channelGroup = new NotificationChannelGroup(account.getIdentifier(),
                account.getFullName());/*from   ww w . j a v  a 2s.  co m*/

        //noinspection ConstantConditions
        notificationManager.createNotificationChannelGroup(channelGroup);

        for (int i = 0; i < channelIds.length; i++) {
            String id = channelIds[i];
            String name = context.getString(channelNames[i]);
            String description = context.getString(channelDescriptions[i]);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(id, name, importance);

            channel.setDescription(description);
            channel.enableLights(true);
            channel.setLightColor(0xFF2B90D9);
            channel.enableVibration(true);
            channel.setShowBadge(true);
            channel.setGroup(account.getIdentifier());
            channels.add(channel);
        }

        //noinspection ConstantConditions
        notificationManager.createNotificationChannels(channels);

    }
}

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 v a 2 s.c om
            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);
    }
}