ekasetiawans / flutter_background_service

269 stars 192 forks source link

Fatal Exception: java.lang.RuntimeException: Unable to start receiver id.flutter.flutter_background_service.WatchdogReceiver #315

Open cameloott opened 1 year ago

cameloott commented 1 year ago

I have an issue. My background function does not work after a while when app goes to background mode. These are error codes we found in Firebase Crashlytics.

Fatal Exception: java.lang.RuntimeException: Unable to start receiver id.flutter.flutter_background_service.WatchdogReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.pati.chalokamao.app/id.flutter.flutter_background_service.BackgroundService }: app is in background uid UidRecord{499a372 u0a932 RCVR idle change:uncached procs:1 proclist:22933, seq(0,0,0)} at android.app.ActivityThread.handleReceiver(ActivityThread.java:3612) at android.app.ActivityThread.access$1300(ActivityThread.java:237) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7050) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

Caused by java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.pati.chalokamao.app/id.flutter.flutter_background_service.BackgroundService }: app is in background uid UidRecord{499a372 u0a932 RCVR idle change:uncached procs:1 proclist:22933, seq(0,0,0)} at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666) at android.app.ContextImpl.startService(ContextImpl.java:1611) at android.content.ContextWrapper.startService(ContextWrapper.java:677) at android.content.ContextWrapper.startService(ContextWrapper.java:677) at id.flutter.flutter_background_service.WatchdogReceiver.onReceive(:47) at android.app.ActivityThread.handleReceiver(ActivityThread.java:3603) at android.app.ActivityThread.access$1300(ActivityThread.java:237) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7050) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

AmmarHammoud commented 1 year ago

Have you found the solution?, I have the same issue.

Akash-github-project commented 1 year ago

From the error message it seems you are trying to start the service when app is in background. Android system doesn't allows that. Start your service beforehand when you start the application.

AmmarHammoud commented 1 year ago

I'm trying to add push notifications feature, so I want the device to receive notifications even if it's not running. Would explain more please.

Akash-github-project commented 1 year ago

Your best bet would be using firebase push notifications

AmmarHammoud commented 1 year ago

Unfortunately, firebase is banned in my country. Still, I'm using socket.io, but facing this exception is so annoying and frustrating.

Akash-github-project commented 1 year ago

Don't try to run foreground service just to send push notifications. Firebase - does this for you in a much more reliable way.

AmmarHammoud commented 1 year ago

I really want to use Firebase, but as I told you, it's banned in my country.

Akash-github-project commented 1 year ago

Then use platform specific apis. Like for Android you can use the alarm manager and broadcast receiver to schedule notifications at a specific time and show notifications from there. But for iOS I don't have expertise on that.

AmmarHammoud commented 1 year ago

My app is pharmacy management so whenever the user orders a medicine, the notification has to be displayed for the admin of the pharmacy so he either approve or deny. I mean what's the point of the app if the notification will displayed at specific time.. right?

Akash-github-project commented 1 year ago

You can run the service in as a foreground service and from there you can show notifications don't use background service.

Akash-github-project commented 1 year ago

as you mentioned you are using socket .io if you are using foreground service then you can listen from there and show notifications.

AmmarHammoud commented 1 year ago

This won't work if the app isn't running...

Akash-github-project commented 1 year ago

Foreground services run as independent processes from the app. They wont close if the app is closed

Akash-github-project commented 1 year ago

Have you used Truecaller or something along the lines? Do you see the sticky notification in your notification section. That's foreground service. They won't close even after the app exit

AmmarHammoud commented 1 year ago

I see your point, you are right, I'll consider this, thank you.

prkay commented 1 year ago

I have raised a PR for this fix. Until owner will merge it you can use flutter_background_service: #MIT LICENSE git: url: https://github.com/ekasetiawans/flutter_background_service.git ref: https://github.com/ekasetiawans/flutter_background_service/commit/78dc190d37001f64a2bb807bffba38fb9143b944 path: packages/flutter_background_service flutter_background_service_android: git: url: https://github.com/ekasetiawans/flutter_background_service.git ref: https://github.com/ekasetiawans/flutter_background_service/commit/37200c0086ffd0284ffb18b033ef90e499536d28 path: packages/flutter_background_service_android