Skip to content

Commit

Permalink
feat: setup JobScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Dec 27, 2024
1 parent 53db36a commit 616eae0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
android:theme="@style/AppTheme"
tools:replace="android:supportsRtl">

<service
android:name="com.expensify.reactnativebackgroundtask.BackgroundJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />

<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.expensify.reactnativebackgroundtask

import android.app.job.JobParameters
import android.app.job.JobService
import android.util.Log
import com.facebook.react.ReactApplication

class BackgroundJobService : JobService() {
override fun onStartJob(params: JobParameters?): Boolean {
// Get the stored taskName
val extras = params?.extras
val taskName = extras?.getString("taskName")

taskName?.let {
val reactApplication = application as ReactApplication
val reactNativeHost = reactApplication.reactNativeHost
val reactContext = reactNativeHost.reactInstanceManager.currentReactContext

reactContext?.getNativeModule(ReactNativeBackgroundTaskModule::class.java)
?.emitOnBackgroundTaskExecution(it)
}
return false
}

override fun onStopJob(params: JobParameters?): Boolean {
// Return true to reschedule the job
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.Callback
import android.app.job.JobScheduler
import android.app.job.JobInfo
import android.content.ComponentName
import android.content.Context
import android.os.PersistableBundle

class ReactNativeBackgroundTaskModule internal constructor(context: ReactApplicationContext) :
ReactNativeBackgroundTaskSpec(context) {
Expand All @@ -14,8 +19,31 @@ class ReactNativeBackgroundTaskModule internal constructor(context: ReactApplica

@ReactMethod
override fun defineTask(taskName: String, taskExecutor: Callback, promise: Promise) {
promise.resolve(taskName);
emitOnBackgroundTaskExecution(taskName);
try {
val jobScheduler = reactApplicationContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val componentName = ComponentName(reactApplicationContext, BackgroundJobService::class.java)

val extras = PersistableBundle().apply {
putString("taskName", taskName)
}

val jobInfo = JobInfo.Builder(taskName.hashCode() and 0xFFFFFF, componentName)
.setPeriodic(15 * 60 * 1000L) // 15 minutes in milliseconds
.setPersisted(true) // Job persists after reboot
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setExtras(extras)
.build()

val resultCode = jobScheduler.schedule(jobInfo)
if (resultCode == JobScheduler.RESULT_SUCCESS) {

promise.resolve(null);
} else {
promise.reject("ERROR", "Failed to schedule job")
}
} catch (e: Exception) {
promise.reject("ERROR", e.message)
}
}

companion object {
Expand Down

0 comments on commit 616eae0

Please sign in to comment.