Java Code Examples for androidx.core.app.NotificationManagerCompat#createNotificationChannel()

The following examples show how to use androidx.core.app.NotificationManagerCompat#createNotificationChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotificationUtils.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
                                                    Context context, String title, String content,
                                                    String summary, String channelId, String channelName,
                                                    String group, int color) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    return new NotificationCompat.Builder(context.getApplicationContext(), channelId)
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.drawable.ic_notification)
            .setColor(color)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .setSummaryText(summary)
                    .bigText(content))
            .setGroup(group)
            .setAutoCancel(true);
}
 
Example 2
Source File: NotificationUtils.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
static NotificationCompat.Builder buildSummaryNotification(Context context, NotificationManagerCompat notificationManager,
                                                           String title, String content, String channelId,
                                                           String channelName, String group, int color) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    return new NotificationCompat.Builder(context, channelId)
            .setContentTitle(title)
            //set content text to support devices running API level < 24
            .setContentText(content)
            .setSmallIcon(R.drawable.ic_notification)
            .setColor(color)
            .setGroup(group)
            .setGroupSummary(true)
            .setAutoCancel(true);
}
 
Example 3
Source File: NotificationUtils.java    From GeometricWeather with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void sendAlertNotification(Context context,
                                          Location location, Alert alert, boolean inGroup) {
    NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        manager.createNotificationChannel(getAlertNotificationChannel(context));
    }

    int notificationId = getAlertNotificationId(context);
    manager.notify(
            notificationId,
            buildSingleAlertNotification(context, location, alert, inGroup, notificationId)
    );

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && inGroup) {
        manager.notify(
                GeometricWeather.NOTIFICATION_ID_ALERT_GROUP,
                buildAlertGroupSummaryNotification(context, location, alert, notificationId)
        );
    }
}
 
Example 4
Source File: SubmitPostService.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ((Infinity) getApplication()).getAppComponent().inject(this);

    mAccessToken = intent.getStringExtra(EXTRA_ACCESS_TOKEN);
    subredditName = intent.getStringExtra(EXTRA_SUBREDDIT_NAME);
    title = intent.getStringExtra(EXTRA_TITLE);
    flair = intent.getParcelableExtra(EXTRA_FLAIR);
    isSpoiler = intent.getBooleanExtra(EXTRA_IS_SPOILER, false);
    isNSFW = intent.getBooleanExtra(EXTRA_IS_NSFW, false);
    int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel serviceChannel = new NotificationChannel(
                NotificationUtils.CHANNEL_SUBMIT_POST,
                NotificationUtils.CHANNEL_SUBMIT_POST,
                NotificationManager.IMPORTANCE_LOW
        );

        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.createNotificationChannel(serviceChannel);
    }

    if (postType == EXTRA_POST_TEXT_OR_LINK) {
        content = intent.getStringExtra(EXTRA_CONTENT);
        kind = intent.getStringExtra(EXTRA_KIND);
        startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting));
        submitTextOrLinkPost();
    } else if (postType == EXTRA_POST_TYPE_IMAGE) {
        mediaUri = intent.getData();
        startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_image));
        submitImagePost();
    } else {
        mediaUri = intent.getData();
        startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID, createNotification(R.string.posting_video));
        submitVideoPost();
    }

    return START_NOT_STICKY;
}
 
Example 5
Source File: BackupService2.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
private void prepareNotificationsStuff() {
    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);
    mNotificationHelper = NotificationHelper.getInstance(this);

    if (Utils.apiIsAtLeast(Build.VERSION_CODES.O)) {
        mNotificationManager.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID, getString(R.string.backup_backup), NotificationManager.IMPORTANCE_DEFAULT));
    }

    startForeground(NOTIFICATION_ID, buildStatusNotification());
}
 
Example 6
Source File: NotificationUtils.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void checkAndSendPrecipitationForecast(Context context,
                                                     Location location, @Nullable Weather oldResult) {
    if (!SettingsOptionManager.getInstance(context).isPrecipitationPushEnabled()
            || location.getWeather() == null) {
        return;
    }
    NotificationManagerCompat manager = NotificationManagerCompat.from(context);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        manager.createNotificationChannel(getAlertNotificationChannel(context));
    }

    Weather weather = location.getWeather();

    SharedPreferences sharedPreferences = context.getSharedPreferences(
            PREFERENCE_SHORT_TERM_PRECIPITATION_ALERT, Context.MODE_PRIVATE);
    String locationKey = sharedPreferences.getString(KEY_PRECIPITATION_LOCATION_KEY, null);
    long date = sharedPreferences.getLong(KEY_PRECIPITATION_DATE, 0);

    if ((!location.getFormattedId().equals(locationKey)
            || isDifferentDays(date, weather.getBase().getPublishTime()))
            && isShortTermLiquid(weather)) {
        manager.notify(
                GeometricWeather.NOTIFICATION_ID_PRECIPITATION,
                getNotificationBuilder(
                        context,
                        R.drawable.ic_precipitation,
                        context.getString(R.string.precipitation_overview),
                        weather.getDailyForecast().get(0).getDate(
                                context.getString(R.string.date_format_widget_long)),
                        context.getString(R.string.feedback_short_term_precipitation_alert),
                        PendingIntent.getActivity(
                                context,
                                GeometricWeather.NOTIFICATION_ID_PRECIPITATION,
                                IntentHelper.buildMainActivityIntent(location),
                                PendingIntent.FLAG_UPDATE_CURRENT
                        )
                ).build()
        );
        sharedPreferences.edit()
                .putString(KEY_PRECIPITATION_LOCATION_KEY, location.getFormattedId())
                .putLong(KEY_PRECIPITATION_DATE, weather.getBase().getPublishTime())
                .apply();
        return;
    }

    if ((oldResult == null
            || isDifferentDays(oldResult.getBase().getPublishTime(), weather.getBase().getPublishTime()))
            && isLiquidDay(weather)) {
        manager.notify(
                GeometricWeather.NOTIFICATION_ID_PRECIPITATION,
                getNotificationBuilder(
                        context,
                        R.drawable.ic_precipitation,
                        context.getString(R.string.precipitation_overview),
                        weather.getDailyForecast().get(0).getDate(
                                context.getString(R.string.date_format_widget_long)),
                        context.getString(R.string.feedback_today_precipitation_alert),
                        PendingIntent.getActivity(
                                context,
                                GeometricWeather.NOTIFICATION_ID_PRECIPITATION,
                                IntentHelper.buildMainActivityIntent(location),
                                PendingIntent.FLAG_UPDATE_CURRENT
                        )
                ).build()
        );
    }
}
 
Example 7
Source File: NotificationCenter.java    From deltachat-android with GNU General Public License v3.0 4 votes vote down vote up
private String getNotificationChannel(NotificationManagerCompat notificationManager, DcChat dcChat) {
    int chatId = dcChat.getId();
    String channelId = CH_MSG_PREFIX;

    if (notificationChannelsSupported()) {
        try {
            // get all values we'll use as settings for the NotificationChannel
            String        ledColor       = Prefs.getNotificationLedColor(context);
            boolean       defaultVibrate = effectiveVibrate(chatId);
            @Nullable Uri ringtone       = effectiveSound(chatId);
            boolean       isIndependent  = requiresIndependentChannel(chatId);

            // get channel id from these settings
            channelId = computeChannelId(ledColor, defaultVibrate, ringtone, isIndependent? chatId : 0);

            // user-visible name of the channel -
            // we just use the name of the chat or "Default"
            // (the name is shown in the context of the group "Chats" - that should be enough context)
            String name = context.getString(R.string.def);
            if (isIndependent) {
                name = dcChat.getName();
            }

            // check if there is already a channel with the given name
            List<NotificationChannel> channels = notificationManager.getNotificationChannels();
            boolean channelExists = false;
            for (int i = 0; i < channels.size(); i++) {
                String currChannelId = channels.get(i).getId();
                if (currChannelId.startsWith(CH_MSG_PREFIX)) {
                    // this is one of the message channels handled here ...
                    if (currChannelId.equals(channelId)) {
                        // ... this is the actually required channel, fine :)
                        // update the name to reflect localize changes and chat renames
                        channelExists = true;
                        channels.get(i).setName(name);
                    } else {
                        // ... another message channel, delete if it is not in use.
                        int currChatId = parseNotificationChannelChatId(currChannelId);
                        if (!currChannelId.equals(computeChannelId(ledColor, effectiveVibrate(currChatId), effectiveSound(currChatId), currChatId))) {
                            notificationManager.deleteNotificationChannel(currChannelId);
                        }
                    }
                }
            }

            // create a channel with the given settings;
            // we cannot change the settings, however, this is handled by using different values for chId
            if(!channelExists) {
                NotificationChannel channel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription("Informs about new messages.");
                channel.setGroup(getNotificationChannelGroup(notificationManager));
                channel.enableVibration(defaultVibrate);
                channel.setShowBadge(true);

                if (!ledColor.equals("none")) {
                    channel.enableLights(true);
                    channel.setLightColor(getLedArgb(ledColor));
                } else {
                    channel.enableLights(false);
                }

                if (ringtone != null && !TextUtils.isEmpty(ringtone.toString())) {
                    channel.setSound(ringtone,
                            new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
                                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
                                    .build());
                }

                notificationManager.createNotificationChannel(channel);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    return channelId;
}