Skip to content

Commit

Permalink
Set notification foreground service behaviour starting with API 31
Browse files Browse the repository at this point in the history
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#167
PiperOrigin-RevId: 557608577
  • Loading branch information
marcbaechinger authored and microkatz committed Sep 29, 2023
1 parent 777cd13 commit 36b00ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ This release includes the following changes since
* Effect:
* Add `VideoFrameProcessor.queueInputBitmap(Bitmap, Iterator<Long>)`
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
Expand Down
1 change: 1 addition & 0 deletions libraries/session/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ public final MediaNotification createNotification(
.setShowWhen(displayElapsedTimeWithChronometer)
.setUsesChronometer(displayElapsedTimeWithChronometer);

if (Util.SDK_INT >= 31) {
Api31.setForegroundServiceBehavior(builder);
}

Notification notification =
builder
.setContentIntent(mediaSession.getSessionActivity())
Expand Down Expand Up @@ -692,6 +696,14 @@ public static void createNotificationChannel(
}
}

@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();
}
Expand Down

0 comments on commit 36b00ab

Please sign in to comment.