diff --git a/android/build.gradle b/android/build.gradle index 93a2797..2e94af7 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -10,13 +10,13 @@ buildscript { apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion '25.0.2' + compileSdkVersion 27 + buildToolsVersion '25.0.3' // Uncomment to develop with yarn link into node_modules // compileOptions.incremental = false defaultConfig { minSdkVersion 16 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 2 versionName "2.0" ndk { @@ -37,7 +37,7 @@ repositories { } dependencies { - compile 'com.firebase:firebase-jobdispatcher:0.7.0' + compile 'com.firebase:firebase-jobdispatcher:0.8.5' compile 'com.facebook.react:react-native:+' } diff --git a/android/src/main/java/com/pilloxa/backgroundjob/ReactNativeEventStarter.java b/android/src/main/java/com/pilloxa/backgroundjob/ReactNativeEventStarter.java index a0e9140..574a8f8 100644 --- a/android/src/main/java/com/pilloxa/backgroundjob/ReactNativeEventStarter.java +++ b/android/src/main/java/com/pilloxa/backgroundjob/ReactNativeEventStarter.java @@ -11,6 +11,11 @@ import com.facebook.react.bridge.Arguments; import com.facebook.react.jstasks.HeadlessJsTaskConfig; import javax.annotation.Nullable; +import android.annotation.SuppressLint; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.os.Build; class ReactNativeEventStarter { private static final String LOG_TAG = ReactNativeEventStarter.class.getSimpleName(); @@ -35,6 +40,29 @@ public void trigger(@NonNull Bundle jobBundle) { public static class MyHeadlessJsTaskService extends HeadlessJsTaskService { private static final String LOG_TAG = MyHeadlessJsTaskService.class.getSimpleName(); + @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()) + .setSmallIcon(R.drawable.ic_notification) + .build(); + + startForeground(1, notification); + } + } + @Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) { Log.d(LOG_TAG, "getTaskConfig() called with: intent = [" + intent + "]"); Bundle extras = intent.getExtras(); @@ -56,7 +84,12 @@ public static void start(Context context, Bundle jobBundle) { "start() called with: context = [" + context + "], jobBundle = [" + jobBundle + "]"); Intent starter = new Intent(context, MyHeadlessJsTaskService.class); starter.putExtras(jobBundle); - context.startService(starter); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(starter); + }else{ + context.startService(starter); + } } } } diff --git a/android/src/main/res/drawable-hdpi/ic_notification.png b/android/src/main/res/drawable-hdpi/ic_notification.png new file mode 100644 index 0000000..f8ff916 Binary files /dev/null and b/android/src/main/res/drawable-hdpi/ic_notification.png differ diff --git a/android/src/main/res/drawable-mdpi/ic_notification.png b/android/src/main/res/drawable-mdpi/ic_notification.png new file mode 100644 index 0000000..44f72e2 Binary files /dev/null and b/android/src/main/res/drawable-mdpi/ic_notification.png differ diff --git a/android/src/main/res/drawable-xhdpi/ic_notification.png b/android/src/main/res/drawable-xhdpi/ic_notification.png new file mode 100644 index 0000000..300cc22 Binary files /dev/null and b/android/src/main/res/drawable-xhdpi/ic_notification.png differ diff --git a/android/src/main/res/drawable-xxhdpi/ic_notification.png b/android/src/main/res/drawable-xxhdpi/ic_notification.png new file mode 100644 index 0000000..fd52ea1 Binary files /dev/null and b/android/src/main/res/drawable-xxhdpi/ic_notification.png differ