【发布时间】:2021-09-24 15:24:09
【问题描述】:
我从一个线程创建一个通知:
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel chan = new NotificationChannel("MyNotificationChannel","Notification Title", NotificationManager.IMPORTANCE_HIGH);
chan.setSound(null, null);
manager.createNotificationChannel(chan);
final Notification notification =
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? new Notification.Builder(context, "MyNotificationChannel") : new Notification.Builder(context))
.setContentTitle(context.getString(R.string.app_name))
.setContentText("Text")
.setSmallIcon(R.drawable.logo)
.setFullScreenIntent(PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT), true)
.setCategory(Notification.CATEGORY_ALARM)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.build();
((Service) context).startForeground(12345678, notification);
当我尝试删除有关 ondestroy 活动的通知时,它适用于大多数设备,但适用于某些摩托罗拉设备或搭载 Android 10 的小米:
protected void onDestroy() {
try {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(12345678);
mNotificationManager.deleteNotificationChannel("MyNotificationChannel");
}catch(Exception e){
e.printStackTrace();
}
}
这个异常通过了,并且通知没有删除,我尝试在线程中删除并进入另一个活动:
java.lang.SecurityException: Not allowed to delete channel "MyNotificationChannel" with a foreground service
【问题讨论】:
-
可能需要提前停止前台服务
-
您的服务是否声明为 START_STICKY ?你在哪里删除通知渠道?应用何时启动?
-
myIntent 不是服务而是活动,我将其显示为弹出窗口。所以用户可以在手机锁定的情况下看到它。我尝试删除启动通知的de服务中的通知和myIntent的ondestroy。在其他手机上通知被删除但在摩托罗拉e6上没有,并抛出不允许删除频道
-
您确定在删除关联的
NotificationChannel之前服务已正确停止吗?一种解决方法是用try / catch包围deleteNotificationChannel()方法 -
我编辑了代码并添加了带有通知 ID 的取消,同样的情况,适用于大多数设备,但不适用于 Moto e。没有服务可以使用通知在设备锁定的情况下将活动发送到前台。
标签: android android-notifications channel securityexception