diff --git a/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt b/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt index 9d6d869ef..e562db118 100644 --- a/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt +++ b/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt @@ -120,9 +120,17 @@ class MusicService : HeadlessJsTaskService() { notificationBuilder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE } val notification = notificationBuilder.build() - startForeground(EMPTY_NOTIFICATION_ID, notification) - @Suppress("DEPRECATION") - stopForeground(true) + try { + // reference exception catch of startForegroundIfNecessary function + startForeground(EMPTY_NOTIFICATION_ID, notification) + @Suppress("DEPRECATION") + stopForeground(true) + } catch (error: Exception) { + Timber.e( + "ForegroundServiceStartNotAllowedException: App tried to start a foreground Service when it was not allowed to do so.", + error + ) + } } @MainThread