Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException

2.3k Views Asked by At
Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{294bdda u0 com.xyz.xyz/com.xyz.pushnotification.BookingProgressService}
       at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:1923)
       at android.app.ActivityThread.access$2700(ActivityThread.java:247)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:201)
       at android.os.Looper.loop(Looper.java:288)
       at android.app.ActivityThread.main(ActivityThread.java:7839)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

I'm facing this crash inside my service and that i have wrote on below way

Start service:

if (Build.VERSION.SDK_INT >= 26)
     mContext.startForegroundService(serviceIntent)
else
     mContext.startService(serviceIntent)

Inside Time Tick update or create notification:

registerTimeTickReceiver() method called inside onStartCommand() of Service class

   private fun registerTimeTickReceiver() {
    if (timeTickReceiver != null)
        unregisterTimeTickReceiver()

    timeTickReceiver = object : BroadcastReceiver() {
        override fun onReceive(
            context: Context, intent: Intent
        ) { //trigger the callback on every minutes
            Log.e("BookingProgressService", "Service registerTimeTickReceiver")
            if (intent.action?.compareTo(Intent.ACTION_TIME_TICK) == 0)
                fetchDataForNotification()
        }
    }
    registerReceiver(timeTickReceiver, IntentFilter(Intent.ACTION_TIME_TICK))
}

Inside fetchDataForNotification() added:

startForeground(booking.id, notification)

Note: This crashes I can't able to reproduce from my device but it is reproduced on some of the devices

My requirement is update multiple booking progress notification on every minute If my app is running or not running(Background) Can anyone please help me? or If you know other best approach then very thankful to you guys

0

There are 0 best solutions below