android - Android notification's names on settings like facebook
问题描述
I would like to make users able to disable/enable notifications separately by their functionalities from settings app like Facebook or other applications do.
As image, Facebook shows a title for each notification use case (as you can understand from my terrible English I'm Italian, so screenshot is in Italian too), so users can disable or enable notifications depending on his need.
How can I do it? Can you post me a code example? Thank you very much and tell me if I wasn't clear
解决方案
您需要创建通知渠道。这是我为此目的创建的 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
这是 Javadoc 的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;
}
推荐阅读
- php - 如果值在 foreach 循环 + php 中不匹配,则跳过步骤
- c# - Oracle dB 自动检索新记录
- java - 检查一维数组是否是另一个二维数组的每一行的子集
- svn - svn 合并基于 xml 的文件
- class - 在另一个类中使用类对象?还是只使用字段?
- java - 如何在使用spring和@Transaction时将全局变量从一个类发送到另一个类?
- ruby - XML 解析破碎的文本
- hyperledger-fabric - 如果提交节点与网络断开连接,其他背书节点和提交节点会提交交易吗?
- javascript - 在 Node JS 中创建 API 下载日志文件
- volume - 说话的响度