Reputation: 176
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
Upvotes: 1
Views: 3602
Reputation: 176
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.
Upvotes: 0
Reputation: 1
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
Upvotes: 0