1

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
user3486626
  • 139
  • 1
  • 6
  • Probably need to stop the foreground service in advance – Zain Jul 15 '21 at 22:03
  • Is your service declared as START_STICKY ? Where do you delete notification channel ? When the app is starting ? – StevenTB Jul 20 '21 at 10:22
  • myIntent is not a service is an activity, that i show as a popup. So the user can see it with the phone locked. I try to delete the notification in de services that start the notification and the ondestroy of myIntent. On other phones the notification is deleted but on motorola e6 no, and throw the not allowd to delete channel – user3486626 Jul 20 '21 at 16:01
  • Are you sure that the service is correctly stopped before deleting the associated `NotificationChannel` ? A workaround is to surround the `deleteNotificationChannel()` method with a `try / catch` – StevenTB Jul 20 '21 at 16:53
  • I edited the code and add the cancel with the notification id and the same hapens, works on most devices but not on Moto e. There is no service y use the notification to send an activity to the foreground with the device locked. – user3486626 Jul 21 '21 at 20:01
  • did you found an answer to this problem? – Mohammad Davari Sep 01 '21 at 09:47

2 Answers2

0

There was similar thread elsewhere as well. What worked for me was that i neatly caught the exception using a try catch surround block and moved ahead. The best practice would be to not delete the channel when it is being used by FG service or stop the service before you go ahead and delete the channel

evoships
  • 1
  • 1
0

I found the solution after too many test. I add this to AndroidManifest.xml:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.REORDER_TASKS" />

In the service:

boolean locked = false;
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
    locked = true;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && locked) {
    final NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    try {
        manager.cancel(12345678);
    }catch(Exception e){
        e.printStackTrace();
    }

    NotificationChannel chan;
    final NotificationManager manager =
                                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String channel = "MyNotificationChannel";
    chan = manager.getNotificationChannel(channel);
    if (chan == null){
        chan = new NotificationChannel(
                channel,
                "Notification Title",
                NotificationManager.IMPORTANCE_HIGH);
        chan.setSound(null, null);  // Service manages its own sound.
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        manager.createNotificationChannel(chan);
    }
    PendingIntent pending = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
    final Notification notification =
            (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                    new Notification.Builder(context, channel) :
                    new Notification.Builder(context))
                    .setContentTitle(context.getString(R.string.app_name))
                    .setContentText("Notification text")
                    .setSmallIcon(R.drawable.log_trans_mini)
                    .setFullScreenIntent(pending, true)
                    .setContentIntent(pending)
                    .setCategory(Notification.CATEGORY_ALARM)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setOngoing(true)
                    .setLights(Color.WHITE, 1000, 1000)
                    .build();
    ((Service) context).startForeground(12345678, notification);
} else {
    context.startActivity(myIntent);
}

What a need is to do next is to delete the channel in the devices that i can, because now after you received a notification the channel is always there. In moto g6 i cannot delete the channel still, but the notification is showing.

user3486626
  • 139
  • 1
  • 6