Android 10 java.lang.SecurityException:不允许使用前台服务删除通道 MyNotificationChannel

问题描述

我从一个线程创建一个通知

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,myIntent,PendingIntent.FLAG_UPDATE_CURRENT),true)
                                            .setCategory(Notification.CATEGORY_ALARM)
                                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                                            .setongoing(true)
                                            .build();

((Service) context).startForeground(12345678,notification);

当我尝试删除有关破坏活动的通知时,它适用于大多数设备,但适用于某些摩托罗拉设备或搭载 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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)