Improve notification method

This commit is contained in:
Oizaro 2020-09-17 13:47:02 +02:00
parent a632b004f5
commit ab6078f366

View File

@ -17,27 +17,38 @@ import com.mgoogle.android.gms.R;
public class StatusNotification { public class StatusNotification {
private static int notificationID; private static int notificationID;
private static String notificationChannelID = "";
private static boolean notificationExists = false; private static boolean notificationExists = false;
public static boolean Notify(Context context) { public static void Notify(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && NotificationManagerCompat.from(context).areNotificationsEnabled()) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && NotificationManagerCompat.from(context).areNotificationsEnabled()) { boolean isChannelEnabled = true;
if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) {
if (notificationExists) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationChannelID != "") {
return false; NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(notificationChannelID);
if (notificationChannel.getImportance() != NotificationManager.IMPORTANCE_NONE) {
isChannelEnabled = true;
} else {
isChannelEnabled = false;
}
} }
if (isChannelEnabled) {
if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) {
if (!notificationExists) {
buildStatusNotification(context); buildStatusNotification(context);
}
} else { } else {
if (notificationExists) { if (notificationExists) {
((NotificationManager) ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
notificationExists = false; notificationExists = false;
} }
} }
} }
return false; }
} }
private static void buildStatusNotification(Context context) { private static void buildStatusNotification(Context context) {
@ -48,9 +59,9 @@ public class StatusNotification {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
String notificationChannelID = "foreground-service";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannelID = "foreground-service";
NotificationChannel channel = new NotificationChannel(notificationChannelID, NotificationChannel channel = new NotificationChannel(notificationChannelID,
context.getResources().getString(R.string.notification_service_name), context.getResources().getString(R.string.notification_service_name),
NotificationManager.IMPORTANCE_LOW); NotificationManager.IMPORTANCE_LOW);
@ -67,8 +78,7 @@ public class StatusNotification {
.setContentTitle(context.getResources().getString(R.string.notification_service_content)) .setContentTitle(context.getResources().getString(R.string.notification_service_content))
.setContentText(context.getResources().getString(R.string.notification_service_subcontent)); .setContentText(context.getResources().getString(R.string.notification_service_subcontent));
NotificationManager manager = NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationID, notification.build()); manager.notify(notificationID, notification.build());