bug: [React Native/Android] KeepAliveService never starts when CAMERA is not granted (Android 14+)

Open Beginner friendly
#208 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
android, react-native, typescript
Domain
mobile

Research direction

Start with android/src/main/java/com/cloudflare/realtimekit/KeepAliveService.java and trace onStartCommand() and startMeetingService() to see how the foreground service types are selected. Check the Android permission APIs used in the suggested fix, then verify audio-only startup keeps the microphone service running without camera permission; done means the service starts with the granted types and camera users can still use the camera type.

Written by the indexing model from the issue text.

Description

Package: @cloudflare/realtimekit-react-native 2.0.0. 2.0.1-staging.0 has the same KeepAliveService.java.
Platform: Android 14+ (targetSdk 34+).

What happens

KeepAliveService.onStartCommand() always calls

startForeground(NOTIFICATION_ID, notification,
    FOREGROUND_SERVICE_TYPE_CAMERA | FOREGROUND_SERVICE_TYPE_MICROPHONE | FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);

On Android 14+, every type passed to startForeground() must meet its runtime prerequisites: camera needs the CAMERA permission granted, and microphone needs RECORD_AUDIO (docs). In an audio-only meeting, or whenever the user hasn't granted camera, the call throws. The catch (RuntimeException e) then logs startForeground failed, stopping service and calls stopSelf().

The result is that the background-support feature added in 2.0.0 silently does nothing. When the app goes to the background, the microphone is no longer covered by a microphone foreground service, so the other side stops hearing the user.

How to observe (derived from the source; not captured on a device)
  1. Use useRealtimeKitClient() with the default keepAliveService and join a meeting with audio: true, video: false, without ever granting camera.
  2. Run adb logcat -s KeepAliveService. The expected line is startForeground failed, stopping service: …, and no ongoing-call notification should appear.
  3. Background the app while talking. Without a microphone foreground service, Android's while-in-use rules cut microphone access, so the remote side should stop hearing you.
Expected

The service starts with the types whose permissions are actually granted.

Suggested fix

Build the type mask from the granted permissions. Video users keep the camera type; audio-only users still get microphone + mediaPlayback:

--- a/android/src/main/java/com/cloudflare/realtimekit/KeepAliveService.java
+++ b/android/src/main/java/com/cloudflare/realtimekit/KeepAliveService.java
@@ -1,5 +1,6 @@
 package com.cloudflare.realtimekit;
 
+import android.Manifest;
 import android.app.Notification;
 import android.app.NotificationChannel;
 import android.app.NotificationManager;
@@ -18,6 +19,7 @@
 import android.util.Log;
 
 import androidx.core.app.NotificationCompat;
+import androidx.core.content.ContextCompat;
 
 /**
  * Foreground service that keeps the app alive in the background during a
@@ -93,10 +95,7 @@
             Notification notification = buildNotification();
             try {
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
-                    startForeground(NOTIFICATION_ID, notification,
-                            ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA
-                            | ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
-                            | ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
+                    startForeground(NOTIFICATION_ID, notification, foregroundServiceTypes());
                 } else {
                     startForeground(NOTIFICATION_ID, notification);
                 }
@@ -109,6 +108,23 @@
         return START_NOT_STICKY;
     }
 
+    // Android 14+ rejects camera/microphone types whose runtime permission is not granted.
+    private int foregroundServiceTypes() {
+        int types = ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK;
+        if (isGranted(Manifest.permission.RECORD_AUDIO)) {
+            types |= ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE;
+        }
+        if (isGranted(Manifest.permission.CAMERA)) {
+            types |= ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA;
+        }
+        return types;
+    }
+
+    private boolean isGranted(String permission) {
+        return ContextCompat.checkSelfPermission(this, permission)
+                == PackageManager.PERMISSION_GRANTED;
+    }
+
     // -----------------------------------------------------------------------
 
     private void createNotificationChannel() {

The manifest entry (camera|microphone|mediaPlayback) can stay as is, because a subset of the declared types is allowed.

One limitation: startMeetingService() runs once at the end of setupStreams(), after setupPermissions(). If camera is granted later in the meeting, the running service won't have the camera type until it is started again. Calling startForeground() again with the new mask after a later permission grant would cover that.

Dominant language
TypeScript
Stars
74
Forks
26
Avg merge
1d 2h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/realtimekit-ui

All issues in cloudflare/realtimekit-ui

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.