Repository navigation
Add background audio with foreground service and CallKit session #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
android/src/main/kotlin/io/fishjam/client/foregroundservice/FishjamForegroundService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package io.fishjam.client.foregroundservice | ||
|
|
||
| import android.app.Notification | ||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager | ||
| import android.app.PendingIntent | ||
| import android.app.Service | ||
| import android.content.Intent | ||
| import android.content.pm.ServiceInfo | ||
| import android.os.Build | ||
| import android.os.IBinder | ||
| import android.util.Log | ||
|
|
||
| /** | ||
| * Foreground service that keeps the app process, and so the call, running while the app is in the | ||
| * background. The app declares it in its AndroidManifest.xml. | ||
| */ | ||
| class FishjamForegroundService : Service() { | ||
| override fun onBind(intent: Intent?): IBinder? = null | ||
|
|
||
| override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
| if (intent != null) { | ||
| startInForeground(intent) | ||
| } | ||
| return START_NOT_STICKY | ||
| } | ||
|
|
||
| private fun startInForeground(intent: Intent) { | ||
| val channelId = intent.getStringExtra(EXTRA_CHANNEL_ID) ?: return | ||
| val channelName = intent.getStringExtra(EXTRA_CHANNEL_NAME) ?: return | ||
| val importance = intent.getStringExtra(EXTRA_IMPORTANCE) ?: "high" | ||
| val types = intent.getIntArrayExtra(EXTRA_TYPES)?.fold(0) { acc, type -> acc or type } ?: 0 | ||
|
|
||
| createChannel(channelId, channelName, importance) | ||
| val notification = buildNotification( | ||
| channelId, | ||
| intent.getStringExtra(EXTRA_TITLE), | ||
| intent.getStringExtra(EXTRA_CONTENT), | ||
| importance, | ||
| intent.getBooleanExtra(EXTRA_ONLY_ALERT_ONCE, false), | ||
| ) | ||
|
|
||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { | ||
| startForeground(NOTIFICATION_ID, notification) | ||
| return | ||
| } | ||
| try { | ||
| startForeground(NOTIFICATION_ID, notification, types) | ||
| } catch (e: SecurityException) { | ||
| // Android 14+ refuses mediaProjection before the user consents to screen capture. | ||
| // A service that never reaches the foreground crashes the app, so retry without it. | ||
| val withoutProjection = types and ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION.inv() | ||
| Log.w(TAG, "Could not start with types $types, retrying with $withoutProjection", e) | ||
| startForeground(NOTIFICATION_ID, notification, withoutProjection) | ||
|
Magmusacy marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| private fun buildNotification( | ||
| channelId: String, | ||
| title: String?, | ||
| content: String?, | ||
| importance: String, | ||
| onlyAlertOnce: Boolean, | ||
| ): Notification { | ||
| val launchIntent = packageManager.getLaunchIntentForPackage(packageName)?.apply { | ||
| addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
| } | ||
| val contentIntent = launchIntent?.let { | ||
| PendingIntent.getActivity( | ||
| this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, | ||
| ) | ||
| } | ||
| val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| Notification.Builder(this, channelId) | ||
| } else { | ||
| @Suppress("DEPRECATION") | ||
| Notification.Builder(this).setPriority( | ||
| if (importance == "low") Notification.PRIORITY_LOW else Notification.PRIORITY_HIGH, | ||
| ) | ||
| } | ||
| return builder | ||
| .setContentTitle(title) | ||
| .setContentText(content) | ||
| .setContentIntent(contentIntent) | ||
| .setSmallIcon(android.R.drawable.ic_dialog_info) | ||
| .setOnlyAlertOnce(onlyAlertOnce) | ||
| .setOngoing(true) | ||
| .build() | ||
| } | ||
|
|
||
| private fun createChannel(channelId: String, channelName: String, importance: String) { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return | ||
| val level = if (importance == "low") { | ||
| NotificationManager.IMPORTANCE_LOW | ||
| } else { | ||
| NotificationManager.IMPORTANCE_HIGH | ||
| } | ||
| getSystemService(NotificationManager::class.java) | ||
| ?.createNotificationChannel(NotificationChannel(channelId, channelName, level)) | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "FishjamForegroundService" | ||
| private const val NOTIFICATION_ID = 0x46_4A_46_53 | ||
|
|
||
| const val EXTRA_CHANNEL_ID = "channelId" | ||
| const val EXTRA_CHANNEL_NAME = "channelName" | ||
| const val EXTRA_TITLE = "notificationTitle" | ||
| const val EXTRA_CONTENT = "notificationContent" | ||
| const val EXTRA_IMPORTANCE = "importance" | ||
| const val EXTRA_ONLY_ALERT_ONCE = "onlyAlertOnce" | ||
| const val EXTRA_TYPES = "foregroundServiceTypes" | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.