@@ -48,16 +48,46 @@ private boolean isIgnoringBatteryOptimizations() {
48
48
return powerManager .isIgnoringBatteryOptimizations (getPackageName ());
49
49
}
50
50
51
+ private static String getServiceName (Service service ) {
52
+ String serviceName = null ;
53
+ try {
54
+ ForegroundServiceInfo annotation = service .getClass ().getAnnotation (ForegroundServiceInfo .class );
55
+ if (annotation != null ) {
56
+ if (annotation .res () != 0 ) {
57
+ try {
58
+ serviceName = service .getString (annotation .res ());
59
+ } catch (Exception ignored ) {
60
+ }
61
+ }
62
+ if (serviceName == null ) {
63
+ serviceName = annotation .value ();
64
+ }
65
+ }
66
+ } catch (Exception ignored ) {
67
+ }
68
+ if (serviceName == null ) {
69
+ serviceName = service .getClass ().getSimpleName ();
70
+ }
71
+ return serviceName ;
72
+ }
73
+
51
74
public static void completeForegroundService (Service service , Intent intent , String tag ) {
52
75
if (android .os .Build .VERSION .SDK_INT >= Build .VERSION_CODES .M
53
76
&& intent != null
54
77
&& intent .getBooleanExtra (EXTRA_FOREGROUND , false )) {
55
- Log .d (tag , "Started in foreground mode." );
56
- service .startForeground (tag .hashCode (), buildForegroundNotification (service ));
78
+ String serviceName = getServiceName (service );
79
+ Log .d (tag , "Started " + serviceName + " in foreground mode." );
80
+ try {
81
+ Notification notification = buildForegroundNotification (service , serviceName );
82
+ service .startForeground (serviceName .hashCode (), notification );
83
+ Log .d (tag , "Notification: " + notification .toString ());
84
+ } catch (Exception e ) {
85
+ Log .w (tag , e );
86
+ }
57
87
}
58
88
}
59
89
60
- private static Notification buildForegroundNotification (Context context ) {
90
+ private static Notification buildForegroundNotification (Context context , String serviceName ) {
61
91
Intent notificationIntent = new Intent ();
62
92
notificationIntent .setAction (Settings .ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS );
63
93
notificationIntent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
@@ -70,6 +100,7 @@ private static Notification buildForegroundNotification(Context context) {
70
100
context .getResources ().getString (R .string .notification_service_name ),
71
101
NotificationManager .IMPORTANCE_LOW );
72
102
Channel .setShowBadge (false );
103
+ Channel .setVibrationPattern (new long []{0 });
73
104
Channel .setLockscreenVisibility (0 );
74
105
context .getSystemService (NotificationManager .class ).createNotificationChannel (Channel );
75
106
}
0 commit comments