From 6bd3e53e8d7ab09cf4f656bac335e7a139a6ab84 Mon Sep 17 00:00:00 2001 From: bachinger Date: Wed, 16 Aug 2023 22:48:06 +0100 Subject: [PATCH] Set notification foreground service behaviour starting with API 31 When the foreground service type is set to `mediaPlayiback` this shouldn't be required. However, some reports from users say that setting the foreground service behavor fixes some problems on some devices. Setting it explicitely with this change makes sure users don't have to do that workaround themselves. Issue: androidx/media#167 PiperOrigin-RevId: 557608577 --- RELEASENOTES.md | 3 +++ libraries/session/build.gradle | 1 + .../session/DefaultMediaNotificationProvider.java | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index d3faefa567..fa3a6d6e35 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -145,6 +145,9 @@ This release includes the following changes since * Effect: * Add `VideoFrameProcessor.queueInputBitmap(Bitmap, Iterator)` queuing bitmap input by timestamp. + * Set the notifications foreground service behavior to + `FOREGROUND_SERVICE_IMMEDIATE` in `DefaultMediaNotificationProvider` + ([#167](https://github.com/androidx/media/issues/167)). * UI: * Add a `Player.Listener` implementation for Wear OS devices that handles playback suppression due to diff --git a/libraries/session/build.gradle b/libraries/session/build.gradle index df6c048d81..2a09cc7658 100644 --- a/libraries/session/build.gradle +++ b/libraries/session/build.gradle @@ -43,6 +43,7 @@ dependencies { compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion implementation 'androidx.collection:collection:' + androidxCollectionVersion implementation 'androidx.media:media:' + androidxMediaVersion + implementation 'androidx.core:core:' + androidxCoreVersion androidTestImplementation 'com.squareup.okhttp3:mockwebserver:' + okhttpVersion androidTestImplementation project(modulePrefix + 'test-utils') androidTestImplementation project(modulePrefix + 'test-data') diff --git a/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java b/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java index b9f7b3debb..19bc2b7dc7 100644 --- a/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java +++ b/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java @@ -371,6 +371,10 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi .setShowWhen(displayElapsedTimeWithChronometer) .setUsesChronometer(displayElapsedTimeWithChronometer); + if (Util.SDK_INT >= 31) { + Api31.setForegroundServiceBehavior(builder); + } + Notification notification = builder .setContentIntent(mediaSession.getSessionActivity()) @@ -692,6 +696,14 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi } } + @RequiresApi(31) + private static class Api31 { + @DoNotInline + public static void setForegroundServiceBehavior(NotificationCompat.Builder builder) { + builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE); + } + } + private static String getBitmapLoadErrorMessage(Throwable throwable) { return "Failed to load bitmap: " + throwable.getMessage(); }