Skip to content

Support lock-screen media controls on the existing Android foreground notification - #30

Open
tifroz wants to merge 3 commits into
skiptools:mainfrom
tifroz:tifroz/android-background-notifications
Open

tifroz wants to merge 3 commits into
skiptools:mainfrom
tifroz:tifroz/android-background-notifications

Conversation

@tifroz

@tifroz tifroz commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Skip Pull Request Checklist:

  • REQUIRED: I have tested my change locally with swift test
  • OPTIONAL: I have tested my change on an iOS simulator or device
  • OPTIONAL: I have tested my change on an Android emulator or device

  • AI was used to generate or assist with generating this PR. Please specify below how you used AI to help you, and what steps you have taken to manually verify the changes.

Codex generated the code, manually validated from a fuse app on android + ios

Summary

Lock-screen controls during remote playback

This change lets the app attach its Android MediaSession to a foreground notification, so Android can show media controls on a locked screen (e.g. A music app playing in the background). The app supplies the media title, playback state, and handlers for controls such as play, pause, and seek. SkipDevice keeps the customized notification visible while ordinary requests start and end. The app can refresh it when playback changes and remove the customization when casting ends.

Source files

  • Sources/SkipDevice/BackgroundActivity.swift, Sources/SkipDevice/Skip/AndroidBackgroundActivityNotifications.kt, and README.md

Tests

  • Added customizedActivityStartsFirstAndEndsFirst and customizedActivityStartsLastAndEndsLast in Tests/SkipDeviceTests/Skip/AndroidBackgroundActivityNotificationsTest.kt. They check which notification stays visible, refreshed content, default restoration, and that refreshing an inactive ID sends no service intent.
  • Updated Tests/SkipDeviceTests/Skip/AndroidManifest.xml with the test service permissions and Tests/SkipDeviceTests/Skip/skip.yml to run the Robolectric tests in the JVM suite.

Background work could begin before Android confirmed startup

This PR also improves the existing service for any background activity. BackgroundActivity.begin used to return as soon as it sent the service intent. It now waits for foreground promotion and any requested partial CPU wake lock, and throws on failure or a ten-second acknowledgement timeout. Concurrent requests share the lock and combine their service types. Callers can check BackgroundActivity.androidStatus(for: id) after startup to detect a service timeout or destruction.

Source files

  • Sources/SkipDevice/BackgroundActivity.swift and README.md

Tests

  • Updated Tests/SkipDeviceTests/SkipDeviceTests.swift. testBackgroundActivityRequestDefaults and testBackgroundActivityRequestCustomValues check that the wake lock is opt-in. The new testAndroidStatusForUnknownActivity checks the Android and Apple results for an unknown identifier.
  • Both new tests in Tests/SkipDeviceTests/Skip/AndroidBackgroundActivityNotificationsTest.kt check running and ended status, combined service types, and lock ownership as requests finish.

Usage Example

In transpiled Swift, call this helper on the Android main thread with the app's media-session token. Pass the casting request's notificationID as the first argument.

#if SKIP
import android.app.Notification
import android.content.Context
import android.media.session.MediaSession
import skip.device.AndroidBackgroundActivityNotifications

func registerMediaNotification(_ id: Int, context: Context, token: MediaSession.Token) {
    AndroidBackgroundActivityNotifications.register(id) { base in
        let builder = Notification.Builder.recoverBuilder(context, base.build())
        return builder.setStyle(Notification.MediaStyle().setMediaSession(token)).build()
    }
}
#endif

API Shape

  • Android adds AndroidBackgroundActivityNotifications.register(id, customize), refresh(id), and remove(id), callable from Kotlin or transpiled Swift. The callback accepts a NotificationCompat.Builder and returns a Notification. A media app can attach a session through this general notification callback.
  • Swift adds BackgroundActivityRequest.requiresAndroidWakeLock: Bool (default false) and BackgroundActivity.androidStatus(for identifier: String) -> String. Status is unavailable on Apple platforms.

Testing

Ran from a clean checkout. macOS XCTest passed 10 tests, Robolectric passed 13, and a Samsung SM-S721U running Android 17 passed 11. Each suite skipped the existing location test. Both notification scenarios ran under Robolectric on API 35. The README's Swift media-notification example also compiled for Android in a separate consumer module.

Limitations

Hosts that request a wake lock must declare android.permission.WAKE_LOCK. Media sessions and playback actions belong to the host app. Hardware verification of lock-screen playback controls is outside this test coverage.

@tifroz tifroz changed the title Fix early startup reporting and notification replacement for Android background activities Support lock-screen media controls on the existing Android foreground notification Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant