Notify after disable/enable app notifications

This commit is contained in:
Oizaro 2020-09-18 21:45:59 +02:00
parent 8585f2ffc7
commit c268e68b2a
1 changed files with 20 additions and 8 deletions

View File

@ -22,8 +22,7 @@ public class StatusNotification {
private static boolean notificationExists = false;
public static void Notify(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isChannelEnabled = true;
@ -38,20 +37,33 @@ public class StatusNotification {
if (isChannelEnabled) {
if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) {
if (!notificationExists) {
buildStatusNotification(context);
boolean resetNotification = false;
if (!NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled()) {
resetNotification = true;
} else {
if (notificationExists && resetNotification) {
destroyNotification(context);
resetNotification = false;
} else {
buildStatusNotification(context);
}
}
} else {
if (notificationExists) {
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
notificationChannelID = "";
notificationExists = false;
destroyNotification(context);
}
}
}
}
}
private static void destroyNotification(Context context) {
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
notificationChannelID = "";
notificationExists = false;
}
private static void buildStatusNotification(Context context) {
notificationID = 1;
@ -84,4 +96,4 @@ public class StatusNotification {
notificationExists = true;
}
}
}