googlearchive / firebase-jobdispatcher-android

DEPRECATED please see the README.md below for details.
Apache License 2.0
1.79k stars 209 forks source link

Firebase periodic job runs only two times in 24 hr #235

Closed jarvis432 closed 4 years ago

jarvis432 commented 6 years ago

I have a service to get the current location of the user and uploading the data to firebase database. When my app is in active state it runs perfectly fine but when app is killed it runs for some time around 20 times and then stops and then runs after 12 hr again for around 20 times and then stops . This is how I schedule my job.

var driver : Driver = GooglePlayDriver(this) var firebaseJobDispatcher : FirebaseJobDispatcher = FirebaseJobDispatcher(driver) var myJob : Job = firebaseJobDispatcher.newJobBuilder() .setService(JobSchedulerService::class.java) .setTag("firstjob") .setConstraints(Constraint.ON_ANY_NETWORK) .setLifetime(Lifetime.FOREVER) .setRecurring(true) .setConstraints(Constraint.DEVICE_IDLE,Constraint.ON_UNMETERED_NETWORK) .setTrigger(periodicTrigger(20,1)) .setReplaceCurrent(false) .build()

    firebaseJobDispatcher.mustSchedule(myJob)

And this is my service class:

class JobSchedulerService : JobService(), GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

var mLocationRequest: LocationRequest? = null
var mGoogleApiClient: GoogleApiClient? = null
var listener: MyLocationListener? = null

override fun onConnectionFailed(p0: ConnectionResult) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun onConnected(p0: Bundle?) {

    listener = MyLocationListener()

    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, listener)

    }

}

override fun onConnectionSuspended(p0: Int) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

private val mJobHandler = Handler(Handler.Callback { msg ->
    Log.e("MapsDemo ","jobStarted")
    jobFinished(msg.obj as JobParameters, false)
    true
})

@SuppressLint("RestrictedApi")
override fun onStartJob(p0: JobParameters?): Boolean {
    buildGoogleApiClient()
    listener = MyLocationListener()
    mLocationRequest = LocationRequest()
    mLocationRequest?.interval = 2000
    mLocationRequest?.fastestInterval = 2000
    mLocationRequest?.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
    mJobHandler.sendMessage(Message.obtain(mJobHandler, 1, p0))
    return true
}

override fun onStopJob(p0: JobParameters?): Boolean {

    Log.e("MapsDemo","JobStopped")

    return false
}

fun buildGoogleApiClient() {
    mGoogleApiClient = GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build()
    mGoogleApiClient?.connect()

}

override fun onCreate() {
    super.onCreate()

}

class MyLocationListener : LocationListener {

    var mFirebaseInstance: FirebaseDatabase? = null
    var mFirebaseDatabase : DatabaseReference? = null
    var userId : String? = null
    var mLastLocation: Location? = null
    override fun onLocationChanged(p0: Location?) {

        mLastLocation = p0

        var latLng = LatLng(p0!!.getLatitude(), p0.getLongitude())
        val markerOptions = MarkerOptions()
        markerOptions?.position(latLng)

        if (FirebaseAuth.getInstance().currentUser != null) {
            userId = FirebaseAuth.getInstance().currentUser!!.uid

        }
        mFirebaseInstance = FirebaseDatabase.getInstance()

        mFirebaseDatabase = mFirebaseInstance!!.getReference("users")

        var lat = mLastLocation?.latitude.toString()
        var long = mLastLocation?.longitude.toString()
        Log.e("Latitude", "" + lat)
        Log.e("Longitude", "" + long)
        createUser(lat, long)
    }

    private fun createUser(lat: String, long: String) {
        if (userId == null){
            userId = FirebaseAuth.getInstance().currentUser?.uid
        }
        var user = User(lat,long)
        mFirebaseDatabase?.child(getDateCurrentTimeZone(System.currentTimeMillis()))?.setValue(user)
    }

    fun getDateCurrentTimeZone(timestamp: Long): String {
    val cal : Calendar = Calendar.getInstance(Locale.ENGLISH)
        cal.timeInMillis = timestamp
        val date :String = DateFormat.format("dd-MM-yyyy HH:mm:ss",cal).toString()
        return date
    }

}
hannesa2 commented 6 years ago

did you found the solution ?

JakeSteam commented 6 years ago

Settings constraints twice can't be a good start..?

samtstern commented 4 years ago

In April 2019 we announced that Firebase Job Dispatcher would be deprecated today, April 7th 2020. For this reason we are going to close all open issues and archive the repository. We will also stop supporting FJD for new app installations (such as those targeting sdk versions greater than Android Q). Existing apps that rely on FJD should continue to function as usual.

While we will no longer work on FJD, we encourage you to migrate to Work Manager which is a superior tool and fully supported by the Android team.

Thank you for taking the time to try the library and provide this feedback. We sincerely appreciate your effort and we apologize for not addressing it in time.