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

Fix background for android 8+ #64

Open
wants to merge 2 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
6 changes: 3 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ buildscript {
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "25.0.2"
compileSdkVersion 27
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,34 @@
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import com.facebook.react.HeadlessJsTaskService;
import java.util.concurrent.TimeUnit;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;

public class HeadlessTaskService extends HeadlessJsTaskService {
private static final String TAG = "BackgroundTask";

@Override
@SuppressLint("WrongConstant")
public void onCreate() {
super.onCreate();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Context mContext = this.getApplicationContext();
String CHANNEL_ID = "Background job";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

Notification notification =
new Notification.Builder(mContext, CHANNEL_ID)
.setContentTitle("Running background job")
.setContentText(mContext.getPackageName())
.build();
startForeground(1, notification);
}
}

@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.Log;
import com.evernote.android.job.util.support.PersistableBundleCompat;
import com.evernote.android.job.Job;
import android.os.Build;

/**
* The single task which this library is able to schedule.
Expand All @@ -32,8 +33,12 @@ protected Result onRunJob(Params params) {
Context context = getContext().getApplicationContext();
Intent service = new Intent(context, HeadlessTaskService.class);
service.putExtras(headlessExtras);
context.startService(service);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(service);
} else {
context.startService(service);
}

return Result.SUCCESS;
}
}
}