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

时间:2017-10-25 09:33:58

标签: android notifications android-8.0-oreo

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

NotificationManagerCompat.from(getContext()).areNotificationsEnabled()

然而,如果用户只停用该频道,我就无法了解它。

有什么想法吗? enter image description here

8 个答案:

答案 0 :(得分:26)

具有向后兼容性:

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();
        }
    }

答案 1 :(得分:10)

查看文档here

  

用户可以修改通知渠道的设置,包括   振动和警报声等行为。你可以打电话给   以下两种方法来发现用户已应用于的设置   通知渠道:

     

要检索单个通知渠道,您可以致电   getNotificationChannel()。检索所有通知渠道   属于您的应用,您可以拨打getNotificationChannels()。后   你拥有NotificationChannel,你可以使用诸如此类的方法   getVibrationPattern()getSound()找出了什么设置   用户目前有。 查看用户是否阻止了通知   频道,您可以拨打getImportance()。如果通知渠道是   已屏蔽,getImportance()返回IMPORTANCE_NONE

答案 2 :(得分:2)

此方法可以提供帮助:

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);
    }

答案 3 :(得分:2)

我认为@itzhar的例子有一个缺陷:当用户在app中完全禁用通知时,我们可能会误报(当频道本身没有被禁用时)。

更新的代码(在Kotlin中)可能如下所示:

test_db=# SELECT t.id, t.version, t.data FROM foo t
test_db-#   JOIN (SELECT id, max(version) as version from foo GROUP BY id) q
test_db-#     ON t.id = q.id AND t.version = q.version;
 id | version | data 
----+---------+------
  3 |       1 | 3.1
  2 |       2 | 2.2
  1 |       3 | 1.3
  4 |       2 | 4.2
(4 rows)

test_db=# SELECT t.id, t.version, t.data FROM foo t
test_db-#   JOIN (SELECT id, max(version) as version from foo GROUP BY id) q
test_db-#     ON t.id = q.id AND t.version = q.version
test_db-#   ORDER BY id DESC LIMIT 2;
 id | version | data 
----+---------+------
  4 |       2 | 4.2
  3 |       1 | 3.1
(2 rows)

答案 4 :(得分:2)

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

在调用方法中:

int evaluate(char board[3][3], char AI)
{
for (int row = 0; row<3; row++)
{
    if (board[row][0] != '_' && board[row][0] == board[row][1] && board[row][1] == board[row][2])
    {
        if (board[row][0]==AI)
        {
            return +10;
        }
        else
        {
            return -10;
        }
    }
}

for (int col = 0; col<3; col++)
{
    if (board[0][col] != '_' && board[0][col] == board[1][col] && board[1][col] == board[2][col])
    {
        if (board[0][col]==AI)
        {
            return +10;
        }

        else
        {
            return -10;
        }
    }
}

if (board[1][1] != '_' && ((board[0][0]==board[1][1] && board[1][1]==board[2][2]) || (board[0][2]==board[1][1] && board[1][1]==board[2][0])))
{
    if (board[1][1]==AI)
    {
        return +10;
    }
    else
    {
        return -10;
    }
}

return 0;
}

int Minimax(char board[3][3], bool AITurn, char AI, char Player, int depth, int alpha, int beta)
{
bool breakout = false;
int score = evaluate(board, AI);

if(score == 10)
{
    return score - depth;
}
else if(score == -10)
{
    return score + depth;
}
else if(NoTilesEmpty(board))
{
    return 0;
}

if(AITurn == true)
{
    int bestvalue = -1024;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j<3; j++)
        {
            if(board[i][j] == '_')
            {
                board[i][j] = AI;
                bestvalue = max(bestvalue, Minimax(board, false, AI, Player, depth+1, alpha, beta));
                alpha = max(bestvalue, alpha);
                board[i][j] = '_';
                if(beta <= alpha)
                {
                    breakout = true;
                    break;
                }
            }
        }
        if(breakout == true)
        {
            break;
        }
    }
    return bestvalue;
}

else if(AITurn == false)
{
    int bestvalue = +1024;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j<3; j++)
        {
            if(board[i][j] == '_')
            {
                board[i][j] = Player;
                bestvalue = min(bestvalue, Minimax(board, true, AI, Player, depth+1, alpha, beta));
                beta = min(bestvalue, beta);
                board[i][j] = '_';
                if(beta <= alpha)
                {
                    breakout = true;
                    break;
                }
            }
        }
        if(breakout == true)
        {
            break;
        }
    }
    return bestvalue;
}
}

在您的课堂上:

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
            isChannelBlocked(CHANNEL_1_ID)) {
        openChannelSettings(CHANNEL_1_ID);
        return;
    }

答案 5 :(得分:1)

这是我的完整代码:

public static boolean isNotificationChannelEnabled(@NonNull String groupId, @NonNull String... channelIds) {
    boolean appNotificationEnable = NotificationManagerCompat.from(AppContext.getContext()).areNotificationsEnabled();
    if (!appNotificationEnable) {
        return false;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) AppContext.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            List<NotificationChannelGroup> groupList = manager.getNotificationChannelGroups();
            for (NotificationChannelGroup group : groupList) {
                if (TextUtils.equals(group.getId(), groupId)) {
                    if (group.isBlocked()) {
                        return false;
                    }
                }
            }
        }

        for (String channelId : channelIds) {
            NotificationChannel channel = manager.getNotificationChannel(channelId);
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                return false;
            }
        }
        return true;
    }

    return false;
}

答案 6 :(得分:0)

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();
        }

答案 7 :(得分:0)

考虑到 areNotificationsEnabled(对于 API 级别 >= 26),只是 Kotlin 中的一个实现。

@TargetApi(Build.VERSION_CODES.O)
private fun isNotificationChannelEnabled(channelId: String): Boolean {
    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    return if (manager.areNotificationsEnabled()){
      val channel = manager.getNotificationChannel(channelId)
      channel.importance != NotificationManager.IMPORTANCE_NONE
    } else false
  }