【问题标题】:Not allowed to delete channel MyNotificationChannel with a foreground service - Android 10 and 11 java.lang.SecurityException不允许使用前台服务删除通道 MyNotificationChannel - Android 10 和 11 java.lang.SecurityException
【发布时间】:2021-09-28 13:36:49
【问题描述】:

我在后台服务运行时创建了通知通道

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel for  notification",
                    NotificationManager.IMPORTANCE_NONE);
        mNotificationManager.createNotificationChannel(channel);
}

onDestroy 函数中,我通过传递相同的频道名称调用以下方法来删除通知:

 notificationManager.deleteNotificationChannel(channelId); 

  

当我尝试在某些运行 Android 10 和 11 的设备上删除该通知时,得到了 SecurityException

【问题讨论】:

    标签: java android notifications android-notifications android-backup-service


    【解决方案1】:

    第一步是在您尝试删除通道时使用 try catch 环绕巧妙地捕获此异常。当我们有一个前台服务正在运行时,我们的想法是不要删除通道。所以要么服务应该停止,要么我们不应该尝试删除频道

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    相关资源
    最近更新 更多