Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -15,6 +17,7 @@
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import androidx.core.content.ContextCompat;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
Expand Down Expand Up @@ -96,13 +99,22 @@ public int onStartCommand(Intent intent, int flags, int startId) {
createNotificationChannel(bgOptions.getTaskTitle(), bgOptions.getTaskDesc()); // Necessary creating channel for API 26+
// Create the notification
final Notification notification = buildNotification(this, bgOptions);
final int foregroundServiceType = bgOptions.getForegroundServiceType();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
&& (foregroundServiceType & ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION) != 0) {
if (!hasLocationPermissions()) {
stopSelf(startId);
return START_NOT_STICKY;
}
}

try {
ServiceCompat.startForeground(
this,
SERVICE_NOTIFICATION_ID,
notification,
bgOptions.getForegroundServiceType()
foregroundServiceType
);
} catch (RuntimeException e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
Expand Down Expand Up @@ -137,4 +149,19 @@ private void createNotificationChannel(@NonNull final String taskTitle, @NonNull
notificationManager.createNotificationChannel(channel);
}
}

/**
* Check if location permissions are granted for a location foreground service.
*/
private boolean hasLocationPermissions() {
// Check location permissions
boolean hasFineLocation = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
boolean hasCoarseLocation = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
boolean hasBackgroundLocation = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;

return hasFineLocation && hasCoarseLocation && hasBackgroundLocation;
}
}