-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(android): create VoipCallService with FOREGROUND_SERVICE_MICROPHONE #7199
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
diegolmello
merged 2 commits into
feat.voip-lib-new
from
feat/voip-android-call-service
Apr 22, 2026
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
157 changes: 157 additions & 0 deletions
157
android/app/src/main/java/chat/rocket/reactnative/voip/VoipCallService.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,157 @@ | ||
| package chat.rocket.reactnative.voip | ||
|
|
||
| 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 | ||
| import androidx.core.app.NotificationCompat | ||
| import chat.rocket.reactnative.MainActivity | ||
|
|
||
| /** | ||
| * Foreground service that keeps the VoIP call alive when the app moves to the background. | ||
| * Required because Android terminates background processes without a foreground service, | ||
| * which would drop the active audio session. | ||
| * | ||
| * Started on call accept, stopped on hangup. | ||
| */ | ||
| class VoipCallService : Service() { | ||
|
|
||
| companion object { | ||
| private const val TAG = "RocketChat.VoipCallService" | ||
| private const val CHANNEL_ID = "voip-call-service" | ||
| private const val CHANNEL_NAME = "VoIP Call" | ||
| private const val NOTIFICATION_ID = 1 | ||
|
|
||
| private const val ACTION_START = "chat.rocket.reactnative.voip.START_SERVICE" | ||
| private const val ACTION_STOP = "chat.rocket.reactnative.voip.STOP_SERVICE" | ||
| const val EXTRA_CALL_ID = "callId" | ||
|
|
||
| private var isRunning = false | ||
|
|
||
| @JvmStatic | ||
| fun startService(context: android.content.Context, callId: String) { | ||
| val intent = Intent(context, VoipCallService::class.java).apply { | ||
| action = ACTION_START | ||
| putExtra(EXTRA_CALL_ID, callId) | ||
| } | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| context.startForegroundService(intent) | ||
| } else { | ||
| context.startService(intent) | ||
| } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun stopService(context: android.content.Context) { | ||
| val intent = Intent(context, VoipCallService::class.java).apply { | ||
| action = ACTION_STOP | ||
| } | ||
| context.stopService(intent) | ||
| } | ||
| } | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
| createNotificationChannel() | ||
| Log.d(TAG, "VoipCallService created") | ||
| } | ||
|
|
||
| override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
| when (intent?.action) { | ||
| ACTION_STOP -> { | ||
| Log.d(TAG, "Stopping VoipCallService") | ||
| stopSelf(startId) | ||
| return START_NOT_STICKY | ||
| } | ||
| ACTION_START -> { | ||
| val callId = intent.getStringExtra(EXTRA_CALL_ID) ?: "unknown" | ||
| Log.d(TAG, "Starting VoipCallService for callId: $callId") | ||
|
diegolmello marked this conversation as resolved.
|
||
| if (!isRunning) { | ||
| isRunning = true | ||
| startForegroundWithNotification(callId) | ||
| } else { | ||
| Log.d(TAG, "Service already running, skipping duplicate start") | ||
| } | ||
| return START_NOT_STICKY | ||
| } | ||
| else -> { | ||
| Log.w(TAG, "Unknown action: ${intent?.action}") | ||
| stopSelf(startId) | ||
| return START_NOT_STICKY | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun startForegroundWithNotification(callId: String) { | ||
| val notification = buildNotification(callId) | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
| startForeground( | ||
| NOTIFICATION_ID, | ||
| notification, | ||
| ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE | ||
| ) | ||
| } else { | ||
| startForeground(NOTIFICATION_ID, notification) | ||
| } | ||
|
|
||
| Log.d(TAG, "Started foreground with notification for callId: $callId") | ||
| } | ||
|
|
||
| private fun buildNotification(callId: String): Notification { | ||
| // Pending intent: tapping the notification opens the app. | ||
| val pendingIntent = PendingIntent.getActivity( | ||
| this, | ||
| 0, | ||
| Intent(this, MainActivity::class.java).apply { | ||
| flags = Intent.FLAG_ACTIVITY_SINGLE_TOP | ||
| }, | ||
| PendingIntent.FLAG_UPDATE_CURRENT or if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
| PendingIntent.FLAG_IMMUTABLE | ||
| } else { | ||
| 0 | ||
| } | ||
| ) | ||
|
|
||
| return NotificationCompat.Builder(this, CHANNEL_ID) | ||
| .setContentTitle("VoIP Call") | ||
| .setContentText("Call in progress") | ||
| .setSmallIcon(getApplicationInfo().icon) | ||
| .setContentIntent(pendingIntent) | ||
| .setOngoing(true) | ||
| .setOnlyAlertOnce(true) | ||
| .setPriority(NotificationCompat.PRIORITY_LOW) | ||
| .setCategory(NotificationCompat.CATEGORY_SERVICE) | ||
| .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) | ||
| .build() | ||
| } | ||
|
|
||
| private fun createNotificationChannel() { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| val channel = NotificationChannel( | ||
| CHANNEL_ID, | ||
| CHANNEL_NAME, | ||
| NotificationManager.IMPORTANCE_LOW | ||
| ).apply { | ||
| description = "VoIP call in progress" | ||
| setShowBadge(false) | ||
| } | ||
| val notificationManager = getSystemService(NotificationManager::class.java) | ||
| notificationManager?.createNotificationChannel(channel) | ||
| } | ||
| } | ||
|
|
||
| override fun onBind(intent: Intent?): IBinder? = null | ||
|
|
||
| override fun onDestroy() { | ||
| isRunning = false | ||
| Log.d(TAG, "VoipCallService destroyed") | ||
| super.onDestroy() | ||
| } | ||
| } | ||
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.