Skip to content

Commit

Permalink
feat: add a broadcaster to properly emit events from module
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Dec 27, 2024
1 parent 6233cfc commit 33d2010
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ package com.expensify.reactnativebackgroundtask

import android.app.job.JobParameters
import android.app.job.JobService
import android.content.Intent
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)
val taskName = params?.extras?.getString("taskName")
val intent = Intent("com.expensify.reactnativebackgroundtask.TASK_ACTION").apply {
putExtra("taskName", taskName)
}
sendBroadcast(intent)

// Job is done, return false if no more work is needed
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@ 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.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.PersistableBundle
import android.util.Log

class ReactNativeBackgroundTaskModule internal constructor(context: ReactApplicationContext) :
ReactNativeBackgroundTaskSpec(context) {

private val taskReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val taskName = intent?.getStringExtra("taskName")
Log.d("ReactNativeBackgroundTaskModule", "Received task: $taskName")
emitOnBackgroundTaskExecution(taskName)
}
}

init {
val filter = IntentFilter("com.expensify.reactnativebackgroundtask.TASK_ACTION")
reactApplicationContext.registerReceiver(taskReceiver, filter)
}

override fun getName(): String {
return NAME
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import com.facebook.react.bridge.Promise
abstract class ReactNativeBackgroundTaskSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {

abstract fun multiply(a: Double, b: Double, promise: Promise)
abstract fun defineTask(taskName: String, taskExecutor: Callback, promise: Promise)
}

0 comments on commit 33d2010

Please sign in to comment.