Android имен уведомлений в настройках, таких как Facebook - PullRequest
0 голосов
/ 13 февраля 2020

Я бы хотел, чтобы пользователи могли отключать / включать уведомления отдельно по своим функциональным возможностям из приложения настроек, как это делают Facebook или другие приложения.

В качестве изображения Facebook показывает заголовок для каждого варианта использования уведомления (так как вы могу понять из моего ужасного англи sh я итальянец, поэтому скриншот тоже на итальянском), поэтому пользователи могут отключать или включать уведомления в зависимости от его потребностей.

Как это сделать? Можете ли вы опубликовать мне пример кода? Большое спасибо и скажите мне, если я не был ясен

enter image description here

1 Ответ

1 голос
/ 14 февраля 2020

Вам необходимо создать каналы уведомлений. Вот метод Util, который я создал для этой цели:

public void createChannel(String channelId, CharSequence channelName, int importance) {
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    channel = new NotificationChannel(channelId, channelName, importance);
    channel.enableLights(true);
    channel.setLightColor(Color.RED);
    channel.enableVibration(true);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    notificationManager.createNotificationChannel(channel);
}

, где channel и notificationManager:

private NotificationChannel channel;
private NotificationManager notificationManager;

Просто назовите каналы с помощью channelId и введите их описание, используя channelName

Вот Javado c для NotificationChannel:

/**
     * Creates a notification channel.
     *
     * @param id The id of the channel. Must be unique per package. The value may be truncated if
     *           it is too long.
     * @param name The user visible name of the channel. You can rename this channel when the system
     *             locale changes by listening for the {@link Intent#ACTION_LOCALE_CHANGED}
     *             broadcast. The recommended maximum length is 40 characters; the value may be
     *             truncated if it is too long.
     * @param importance The importance of the channel. This controls how interruptive notifications
     *                   posted to this channel are.
     */
    public NotificationChannel(String id, CharSequence name, @Importance int importance) {
        this.mId = getTrimmedString(id);
        this.mName = name != null ? getTrimmedString(name.toString()) : null;
        this.mImportance = importance;
    }
...