Deprecate NotificationCollectorMonitorService

Don't start the foreground NotificationCollectorMonitorService unless enabled in the deprecated settings.

The notification service seems to be reliably kept alive by the OS without this workaround, and having a second foreground service is causing a second persistent notification for some users.
This commit is contained in:
Daniele Gobbetti
2025-07-16 17:22:07 +02:00
parent afd529fbf3
commit f78d59d6d4
3 changed files with 39 additions and 22 deletions
@@ -332,33 +332,41 @@ public class GBApplication extends Application {
bif.addAction(BluetoothStateChangeReceiver.ANDROID_BLUETOOTH_DEVICE_ACTION_BATTERY_LEVEL_CHANGED);
registerReceiver(bluetoothStateChangeReceiver, bif);
}
try {
//the following will ensure the notification manager is kept alive
Intent serviceIntent = new Intent(context, NotificationCollectorMonitorService.class);
//ContextCompat starts a background service on android < O automatically despite the method name
ContextCompat.startForegroundService(context, serviceIntent);
} catch (IllegalStateException e) {
String message = e.toString();
final Intent instructionsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://gadgetbridge.org/basics/topics/background-service/"));
final PendingIntent pi = PendingIntentUtils.getActivity(context, 0, instructionsIntent, PendingIntent.FLAG_ONE_SHOT, false);
GB.notify(NOTIFICATION_ID_ERROR,
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.error_background_service))
.setContentText(getString(R.string.error_background_service_reason_truncated))
.setContentIntent(pi)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getString(R.string.error_background_service_reason) + " \"" + message + "\""))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build(), context);
}
startNotificationCollectorMonitorService();
}
BondingUtil.StartObservingAll(getBaseContext());
}
private void startNotificationCollectorMonitorService() {
if (!prefs.getBoolean("prefs_key_enable_deprecated_notificationcollectormonitor", false)) {
return;
}
Log.i(TAG, "Starting the deprecated NotificationCollectorMonitorService foreground service.");
try {
//the following will ensure the notification manager is kept alive
Intent serviceIntent = new Intent(context, NotificationCollectorMonitorService.class);
//ContextCompat starts a background service on android < O automatically despite the method name
ContextCompat.startForegroundService(context, serviceIntent);
} catch (IllegalStateException e) {
String message = e.toString();
final Intent instructionsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://gadgetbridge.org/basics/topics/background-service/"));
final PendingIntent pi = PendingIntentUtils.getActivity(context, 0, instructionsIntent, PendingIntent.FLAG_ONE_SHOT, false);
GB.notify(NOTIFICATION_ID_ERROR,
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.error_background_service))
.setContentText(getString(R.string.error_background_service_reason_truncated))
.setContentIntent(pi)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getString(R.string.error_background_service_reason) + " \"" + message + "\""))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build(), context);
}
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
+2
View File
@@ -4134,4 +4134,6 @@
<string name="devicetype_dotn_p66d">Dotn P66D</string>
<string name="pref_title_time_interval">Time interval</string>
<string name="pref_summary_time_interval">Restrict to specific intervals</string>
<string name="prefs_key_enable_deprecated_notificationcollectormonitor_title">Enable the deprecated Notification Collector Monitor Service</string>
<string name="prefs_key_enable_deprecated_notificationcollectormonitor_summary">Forces the usage of a second foreground service to keep the Notification monitor alive, activate this option if Gadgetbridge stops receiving notifications.\nPlease note that it might cause a second persistent notification to be shown.\nRestart the application after toggling.</string>
</resources>
+7
View File
@@ -451,6 +451,13 @@
app:iconSpaceReserved="false"
app:singleLineTitle="false" />
<SwitchPreferenceCompat
android:key="prefs_key_enable_deprecated_notificationcollectormonitor"
android:layout="@layout/preference_checkbox"
android:summary="@string/prefs_key_enable_deprecated_notificationcollectormonitor_summary"
android:title="@string/prefs_key_enable_deprecated_notificationcollectormonitor_title"
app:iconSpaceReserved="false" />
<PreferenceCategory
android:key="pref_header_media_control"
android:title="@string/pref_deprecated_media_control_title"