Skip to content
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

Add support for >= android 14 #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def safeExtGet(prop, fallback) {
}

android {
compileSdkVersion 34
compileSdkVersion 35

defaultConfig {
minSdkVersion 21
targetSdkVersion 34
targetSdkVersion 35
versionCode 2
versionName "1.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Constants {
static final String ERROR_INVALID_CONFIG = "ERROR_INVALID_CONFIG";
static final String ERROR_SERVICE_ERROR = "ERROR_SERVICE_ERROR";
static final String ERROR_ANDROID_VERSION = "ERROR_ANDROID_VERSION";
static final String ERROR_FGS_TYPE_MISSING = "ERROR_FGS_TYPE_MISSING";

static final String FOREGROUND_SERVICE_BUTTON_PRESSED = "FOREGROUND_SERVICE_BUTTON_PRESSED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Build;
import android.content.pm.ServiceInfo;

import static com.voximplant.foregroundservice.Constants.NOTIFICATION_CONFIG;

Expand All @@ -26,11 +28,16 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (action.equals(Constants.ACTION_FOREGROUND_SERVICE_START)) {
if (intent.getExtras() != null && intent.getExtras().containsKey(NOTIFICATION_CONFIG)) {
Bundle notificationConfig = intent.getExtras().getBundle(NOTIFICATION_CONFIG);
int foregroundServiceType = intent.getIntExtra("foregroundServiceType", 1); // is 1 the best default value we can have???
if (notificationConfig != null && notificationConfig.containsKey("id")) {
Notification notification = NotificationHelper.getInstance(getApplicationContext())
.buildNotification(getApplicationContext(), notificationConfig);

startForeground((int)notificationConfig.getDouble("id"), notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // android 14 and above
startForeground((int)notificationConfig.getDouble("id"), notification, foregroundServiceType);
} else {
startForeground((int)notificationConfig.getDouble("id"), notification);
}
}
}
} else if (action.equals(Constants.ACTION_FOREGROUND_SERVICE_STOP)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import static com.voximplant.foregroundservice.Constants.ERROR_SERVICE_ERROR;
import static com.voximplant.foregroundservice.Constants.NOTIFICATION_CONFIG;
import static com.voximplant.foregroundservice.Constants.FOREGROUND_SERVICE_BUTTON_PRESSED;


import static com.voximplant.foregroundservice.Constants.ERROR_FGS_TYPE_MISSING;

public class VIForegroundServiceModule extends ReactContextBaseJavaModule {

Expand Down Expand Up @@ -70,13 +69,20 @@ public void createNotificationChannel(ReadableMap channelConfig, Promise promise
}

@ReactMethod
public void startService(ReadableMap notificationConfig, Promise promise) {
public void startService(ReadableMap notificationConfig, @Nullable Integer foregroundServiceType, Promise promise) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (foregroundServiceType == null) {
promise.reject(ERROR_FGS_TYPE_MISSING, "VIForegroundService: Foreground service type is required");
return;
}
}

if (notificationConfig == null) {
promise.reject(ERROR_INVALID_CONFIG, "VIForegroundService: Notification config is invalid");
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // if device is running android 8 and above
if (!notificationConfig.hasKey("channelId")) {
promise.reject(ERROR_INVALID_CONFIG, "VIForegroundService: channelId is required");
return;
Expand Down Expand Up @@ -106,18 +112,24 @@ public void startService(ReadableMap notificationConfig, Promise promise) {
Intent intent = new Intent(getReactApplicationContext(), VIForegroundService.class);
intent.setAction(Constants.ACTION_FOREGROUND_SERVICE_START);
intent.putExtra(NOTIFICATION_CONFIG, Arguments.toBundle(notificationConfig));
intent.putExtra("foregroundServiceType", foregroundServiceType);
ComponentName componentName = getReactApplicationContext().startService(intent);

IntentFilter filter = new IntentFilter();
filter.addAction(FOREGROUND_SERVICE_BUTTON_PRESSED);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
getReactApplicationContext().registerReceiver(foregroundReceiver, filter, null, null, Context.RECEIVER_NOT_EXPORTED);
} else {
getReactApplicationContext().registerReceiver(
foregroundReceiver,
filter,
null,
null,
Context.RECEIVER_NOT_EXPORTED
);
} else {
getReactApplicationContext().registerReceiver(foregroundReceiver, filter);
}



if (componentName != null) {
promise.resolve(null);
} else {
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ class VIForegroundService {
/**
* Start foreground service
* @param {NotificationConfig} notificationConfig - Notification config
* @param {number} foregroundServiceType - The foreground service type as described in your manifest file. Required for API 34 and above.
* @return Promise
*/
async startService(notificationConfig) {
async startService(notificationConfig, foregroundServiceType=null) {
if (isIOS) {
console.warn("ForegroundService should be used only on Android platfrom.")
return;
}
return await ForegroundServiceModule.startService(notificationConfig);
return await ForegroundServiceModule.startService(notificationConfig, foregroundServiceType);
}

/**
Expand Down
Loading