Android Facebook 等设置中的通知名称

Android notification's names on settings like facebook

我想让用户能够 disable/enable 通过设置应用程序(如 Facebook 或其他应用程序)的功能单独通知。

如图所示,Facebook 为每个通知用例显示了一个标题(你可以从我糟糕的英语中了解到我是意大利人,所以屏幕截图也是意大利语),因此用户可以根据需要禁用或启用通知.

我该怎么做?你能 post 给我一个代码示例吗? 非常感谢,如果我不清楚请告诉我

您需要创建通知渠道。这是我为此目的创建的 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);
}

其中 channelnotificationManager:

private NotificationChannel channel;
private NotificationManager notificationManager;

只需使用 channelId 命名频道并使用 channelName

给它们一个描述

这是 NotificationChannel:

的 Javadoc
/**
     * 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;
    }