diff --git a/gpslogger/src/main/AndroidManifest.xml b/gpslogger/src/main/AndroidManifest.xml index a189a366e..bcf749f78 100644 --- a/gpslogger/src/main/AndroidManifest.xml +++ b/gpslogger/src/main/AndroidManifest.xml @@ -27,6 +27,8 @@ + + = Build.VERSION_CODES.TIRAMISU) { + permissions.add(Manifest.permission.POST_NOTIFICATIONS); + } if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { // Only on Android 10 (Q), the permission dialog can include an 'Allow all the time' @@ -1501,6 +1507,16 @@ private void startAndBindService() { * Stops the service if it isn't logging. Also unbinds. */ private void stopAndUnbindServiceIfRequired() { + if(!NotificationManagerCompat.from(this).areNotificationsEnabled()) { + // Alright. Why is this needed. + // If the notification permission has been revoked or not granted for whatever reason. + // When the application opens, the service starts, then stops right away. + // Android requires a notification to be shown for a foreground service within 5 seconds. + // So the application crashes and comes back repeatedly. Very weird. + // The answer - if notifications are disabled, don't unbind the service. It will stop on its own. + // Might be related: https://stackoverflow.com/questions/73067939/start-foreground-service-after-notification-permission-was-disabled-causes-crash + return; + } if (session.isBoundToService()) { try { @@ -1514,6 +1530,10 @@ private void stopAndUnbindServiceIfRequired() { if (!session.isStarted()) { LOG.debug("Stopping the service"); try { + // Stop service crashes if the intent is null. lol + if(serviceIntent == null){ + serviceIntent = new Intent(this, GpsLoggingService.class); + } stopService(serviceIntent); } catch (Exception e) { LOG.error("Could not stop the service", e);