Battery saving notification improved

This commit is contained in:
Oizaro 2020-09-19 02:32:11 +02:00
parent 7d6f3d1dea
commit 95543f10a4

View File

@ -17,8 +17,9 @@ import com.mgoogle.android.gms.R;
public class StatusNotification { public class StatusNotification {
private static int notificationID; private static Notification Notification;
private static String notificationChannelID = ""; private static int notificationID = 1;
private static String notificationChannelID = "foreground-service";
private static boolean notificationExists = false; private static boolean notificationExists = false;
public static void Notify(Context context) { public static void Notify(Context context) {
@ -27,74 +28,64 @@ public class StatusNotification {
boolean isChannelEnabled = true; boolean isChannelEnabled = true;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationChannelID != "") { if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(notificationChannelID); NotificationChannel notificationChannel = notificationManager.getNotificationChannel(notificationChannelID);
if (notificationChannel.getImportance() == NotificationManager.IMPORTANCE_NONE) { if (notificationChannel != null
&& notificationChannel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
isChannelEnabled = false; isChannelEnabled = false;
} }
} }
if (isChannelEnabled) { if (NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled()
&& isChannelEnabled) {
if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) { if (!powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) {
boolean resetNotification = false; if (!notificationExists) {
buildStatusNotification(context);
if (!NotificationManagerCompat.from(context.getApplicationContext()).areNotificationsEnabled()) { } else {
resetNotification = true; NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
} else { notificationManager.notify(notificationID, Notification);
if (notificationExists) {
if (resetNotification) {
destroyNotification(context);
resetNotification = false;
} else {
buildStatusNotification(context);
}
} }
}
} else { } else {
if (notificationExists) { if (notificationExists) {
destroyNotification(context); ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).deleteNotificationChannel(notificationChannelID);
}
notificationExists = false;
} }
} }
} }
} }
} }
private static void destroyNotification(Context context) {
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notificationID);
notificationChannelID = "";
notificationExists = false;
}
private static void buildStatusNotification(Context context) { private static void buildStatusNotification(Context context) {
notificationID = 1; Intent notificationIntent = new Intent();
notificationIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
Intent intent = new Intent(); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); PendingIntent notificationPendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
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);
channel.setShowBadge(true); Channel.setShowBadge(false);
channel.setLockscreenVisibility(0); Channel.setLockscreenVisibility(0);
channel.setVibrationPattern(new long[0]); Channel.setVibrationPattern(new long[0]);
context.getSystemService(NotificationManager.class).createNotificationChannel(channel); context.getSystemService(NotificationManager.class).createNotificationChannel(Channel);
} }
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, notificationChannelID) Notification = new NotificationCompat.Builder(context, notificationChannelID)
.setOngoing(true) .setOngoing(true)
.setContentIntent(pendingIntent) .setContentIntent(notificationPendingIntent)
.setSmallIcon(R.drawable.ic_foreground_notification) .setSmallIcon(R.drawable.ic_foreground_notification)
.setStyle(new NotificationCompat.BigTextStyle() .setStyle(new NotificationCompat.BigTextStyle()
.setBigContentTitle(context.getResources().getString(R.string.notification_service_title)) .setBigContentTitle(context.getResources().getString(R.string.notification_service_title))
.bigText(context.getResources().getString(R.string.notification_service_content))); .bigText(context.getResources().getString(R.string.notification_service_content)))
.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notificationID, notification.build());
notificationManager.notify(notificationID, Notification);
notificationExists = true; notificationExists = true;
} }