Android Oreo Notifications - 检查是否启用了特定频道

itz*_*har 19 notifications android android-8.0-oreo

我正在使用此代码段来检查是否启用了通知:

NotificationManagerCompat.from(getContext()).areNotificationsEnabled()
Run Code Online (Sandbox Code Playgroud)

但是,如果用户仅禁用该频道,我无法知道它.

任何的想法? 在此输入图像描述

itz*_*har 38

具有向后兼容性:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 还应该在Oreo +上检查`areNotificationsEnabled`,因为可以在禁用整体通知时启用通道 (5认同)
  • 就像@FlorianWalther 所说的,也应该调用 areNotificationsEnabled()。像这样`return (channel.getImportance() != NotificationManager.IMPORTANCE_NONE) && NotificationManagerCompat.from(context).areNotificationsEnabled();` (2认同)
  • 这是行不通的。我已禁用我的应用程序频道,但仍收到“IMPORTANCE_DEFAULT” (2认同)

dit*_*itn 16

这里查看文档.

用户可以修改通知通道的设置,包括振动和警报声等行为.您可以调用以下两种方法来发现用户已应用于通知通道的设置:

要检索单个通知渠道,您可以致电 getNotificationChannel().要检索属于您的应用的所有通知渠道,您可以致电getNotificationChannels().拥有之后NotificationChannel,您可以使用诸如getVibrationPattern()和之类的方法 getSound()来查找用户当前具有的设置.要确定用户是否阻止了通知渠道,您可以致电getImportance().如果通知通道被阻止,则getImportance()返回IMPORTANCE_NONE.


Flo*_*her 5

使用它来检查是整体禁用通知还是禁用通道,并将用户带到相应的设置:

在调用方法中:

if (!notificationManager.areNotificationsEnabled()) {
        openNotificationSettings();
        return;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
            isChannelBlocked(CHANNEL_1_ID)) {
        openChannelSettings(CHANNEL_1_ID);
        return;
    }
Run Code Online (Sandbox Code Playgroud)

在您的课程中:

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);
}
Run Code Online (Sandbox Code Playgroud)