我正在使用此代码段来检查是否启用了通知:
NotificationManagerCompat.from(getContext()).areNotificationsEnabled()
但是,如果用户仅禁用该频道,我无法知道它。
具有向后兼容性:
public boolean isNotificationChannelEnabled(Context context, @Nullable String channelId){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(!TextUtils.isEmpty(channelId)) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(channelId);
return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
}
return false;
} else {
return NotificationManagerCompat.from(context).areNotificationsEnabled();
}
}
看看文档here。
用户可以修改通知通道的设置,包括振动和警报声等行为。您可以调用以下两种方法来发现用户已应用于通知通道的设置:
要检索单个通知渠道,您可以调用
getNotificationChannel()
。要检索属于您的应用的所有通知渠道,您可以致电getNotificationChannels()
。拥有NotificationChannel
后,您可以使用getVibrationPattern()
和getSound()
等方法来查找用户当前拥有的设置。要确定用户是否阻止了通知渠道,您可以调用getImportance()
。如果通知渠道被阻止,getImportance()
将返回IMPORTANCE_NONE
。
使用此选项可检查整体通知或通道是否已禁用,并将用户带到相应的设置:
在调用方法中:
if (!notificationManager.areNotificationsEnabled()) {
openNotificationSettings();
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
isChannelBlocked(CHANNEL_1_ID)) {
openChannelSettings(CHANNEL_1_ID);
return;
}
在你的班上:
private void openNotificationSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(intent);
} else {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
}
@RequiresApi(26)
private boolean isChannelBlocked(String channelId) {
NotificationManager manager = getSystemService(NotificationManager.class);
NotificationChannel channel = manager.getNotificationChannel(channelId);
return channel != null &&
channel.getImportance() == NotificationManager.IMPORTANCE_NONE;
}
@RequiresApi(26)
private void openChannelSettings(String channelId) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
startActivity(intent);
}
这种方法可以帮助:
public boolean isNotificationChannelDisabled(@NonNull String channelId) {
if(!channelId.equals(EMPTY)) {
NotificationChannel channel = getManager().getNotificationChannel(channelId);
return channel.getImportance() == NotificationManager.IMPORTANCE_NONE;
}
return false;
}
private NotificationManager getManager(@NonNull Context context) {
return mManager(android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
我认为@itzhar的例子有一个缺陷:当用户在app中完全禁用通知时,我们可能会出现误报(当频道本身未被禁用时)。
更新的代码(在Kotlin中)可能如下所示:
fun isNotificationAllowed(context: Context): Boolean {
return if (isOOrLater()) {
areNotificationsEnabled(context) and isChannelDisabled(context)
} else {
areNotificationsEnabled(context)
}
}
private fun areNotificationsEnabled(context: Context) =
NotificationManagerCompat.from(context).areNotificationsEnabled()
@RequiresApi(api = Build.VERSION_CODES.O)
private fun isChannelDisabled(context: Context): Boolean{
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID)
return channel.importance != NotificationManager.IMPORTANCE_NONE
}
private fun isOOrLater() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(NotificationManagerCompat.from(context).areNotificationsEnabled()) {
for (String channelId : channelIds) {
if (!TextUtils.isEmpty(channelId)) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
NotificationChannel channel = manager.getNotificationChannel(channelId);
if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE)
return false;
}
}
}
return true;
}else return false;
}else{
return NotificationManagerCompat.from(context).areNotificationsEnabled();
}