首页 > 解决方案 > Android:如何以编程方式禁用通知通道?

问题描述

我有一个有效的实现,以检查是否启用了通道。

但是有没有办法以编程方式禁用通知通道?

我如何检查通道是否已启用:

    /**
     * Get the setting a user has applied to the notification channel.
     * If the android API level is < 26, it will return true if all notification
     * are enabled in general, false otherwise.
     *
     * @return true if the channel is enabled, false otherwise
     */
    public static boolean isChannelEnabled(String channelId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationManager notificationManager = App.get().getSystemService(NotificationManager.class);
            if (notificationManager == null) {
                return true;
            } else {
                final NotificationChannel c = notificationManager.getNotificationChannel(channelId);
                final boolean overallEnabled = notificationManager.areNotificationsEnabled();
                return overallEnabled && c != null && NotificationManager.IMPORTANCE_NONE != c.getImportance();
            }
        } else {
            return NotificationManagerCompat.from(App.get()).areNotificationsEnabled();
        }
    }


标签: androidnotificationsnotification-channel

解决方案


推荐阅读