Skip to content

Commit

Permalink
fix(android) Use startForegroundService and do not delete the notific…
Browse files Browse the repository at this point in the history
…ation channel until onDestroy (#4105)

See https://developer.android.com/develop/background-work/services/foreground-services\#fgs-prerequisites
    See rationale here https://stackoverflow.com/questions/45525214/are-there-any-benefits-to-using-context-startforegroundserviceintent-instead-o
    Deleting the notification channel while the foreground service is still running is not permitted.
  • Loading branch information
paulrinaldi authored Oct 3, 2024
1 parent 149924f commit 40872f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,11 @@ public void onNullBinding(ComponentName name) {
Intent intent = new Intent(themedReactContext, VideoPlaybackService.class);
intent.setAction(MediaSessionService.SERVICE_INTERFACE);

themedReactContext.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
themedReactContext.startForegroundService(intent);
} else {
themedReactContext.startService(intent);
}

int flags;
if (Build.VERSION.SDK_INT >= 29) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class VideoPlaybackService : MediaSessionService() {

mediaSessionsList[player] = mediaSession
addSession(mediaSession)
startForeground(mediaSession.player.hashCode(), buildNotification(mediaSession))
}

fun unregisterPlayer(player: ExoPlayer) {
Expand Down Expand Up @@ -95,6 +96,10 @@ class VideoPlaybackService : MediaSessionService() {

override fun onDestroy() {
cleanup()
val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANEL_ID)
}
super.onDestroy()
}

Expand Down Expand Up @@ -209,9 +214,6 @@ class VideoPlaybackService : MediaSessionService() {
private fun hideAllNotifications() {
val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancelAll()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.deleteNotificationChannel(NOTIFICATION_CHANEL_ID)
}
}

private fun cleanup() {
Expand Down

0 comments on commit 40872f5

Please sign in to comment.