From e9995b5b5d9a9239a47cdad6b338c8cab206a6f0 Mon Sep 17 00:00:00 2001 From: mayank-paryani <105206321+mayank-paryani@users.noreply.github.com> Date: Fri, 18 Oct 2024 18:09:30 +0530 Subject: [PATCH] Update MusicService.kt to FIX [4.0.1] Crash: com.doublesymmetry.trackplayer.service.MusicService.startAndStopEmptyNotificationToAvoidANR Update MusicService.kt to FIX [4.0.1] Crash: com.doublesymmetry.trackplayer.service.MusicService.startAndStopEmptyNotificationToAvoidANR https://github.com/doublesymmetry/react-native-track-player/issues/2244#ref-commit-5dfc0f0 --- .../trackplayer/service/MusicService.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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