I create a notification from a Thread:
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);
When i try to delete that notification on the ondestroy of the activity it works on most devices but on some Motorola Devices or Xiaomi with Android 10:
protected void onDestroy() {
try {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(12345678);
mNotificationManager.deleteNotificationChannel("MyNotificationChannel");
}catch(Exception e){
e.printStackTrace();
}
}
This exception is through, and the notification is not delete, i try to delete in the thread and into another activities:
java.lang.SecurityException: Not allowed to delete channel "MyNotificationChannel" with a foreground service