DavidBriglio / cordova-plugin-foreground-service

Foreground service with ongoing notification for Android.
MIT License
39 stars 40 forks source link

Not allowed to delete channel foreground.service.channel with a foreground service (With a solution) #25

Closed ArielAleksandrus closed 1 year ago

ArielAleksandrus commented 1 year ago

Describe the bug

FATAL EXCEPTION: main
    Process: live.nlabs.bible, PID: 16338
    java.lang.RuntimeException: Unable to start service com.davidbriglio.foreground.ForegroundService@6aefd8c with Intent { act=start cmp=live.nlabs.bible/com.davidbriglio.foreground.ForegroundService (has extras) }: java.lang.SecurityException: Not allowed to delete channel foreground.service.channel with a foreground service
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5261)
        at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2447)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8757)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
    Caused by: java.lang.SecurityException: Not allowed to delete channel foreground.service.channel with a foreground service
        at android.os.Parcel.createExceptionOrNull(Parcel.java:3023)
        at android.os.Parcel.createException(Parcel.java:3007)
        at android.os.Parcel.readException(Parcel.java:2990)
        at android.os.Parcel.readException(Parcel.java:2932)
        at android.app.INotificationManager$Stub$Proxy.deleteNotificationChannel(INotificationManager.java:4757)
        at android.app.NotificationManager.deleteNotificationChannel(NotificationManager.java:1034)
        at com.davidbriglio.foreground.ForegroundService.startPluginForegroundService(ForegroundService.java:34)
        at com.davidbriglio.foreground.ForegroundService.onStartCommand(ForegroundService.java:18)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5243)
        at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2447) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:226) 
        at android.os.Looper.loop(Looper.java:313) 
        at android.app.ActivityThread.main(ActivityThread.java:8757) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
    Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.notification.NotificationManagerService$12.enforceDeletingChannelHasNoFgService(NotificationManagerService.java:4667)
        at com.android.server.notification.NotificationManagerService$12.deleteNotificationChannel(NotificationManagerService.java:4680)
        at android.app.INotificationManager$Stub.onTransact(INotificationManager.java:2172)
        at android.os.Binder.execTransactInternal(Binder.java:1316)
        at android.os.Binder.execTransact(Binder.java:1280)

To Reproduce Steps to reproduce the behavior:

  1. My App is crashing when I click to play a chapter. HERE is my app's Github

Expected behavior A foreground service started

Smartphone (please complete the following information):

Additional context I managed to fix the problem by using a try/catch

ForegroundService.java:34

try{
    manager.deleteNotificationChannel("foreground.service.channel");
  } catch(Exception e) {
  // just ignore the exception for now. better than crashing my app
  e.printStackTrace();
}
ArielAleksandrus commented 1 year ago

Surrounding that line of code with a try/catch has successfully prevented my app from crashing with no other unexpected behaviour. Seems that it can cause a RuntimeException, so it's better to surround it with try/catch. @DavidBriglio do you think we can update the project?

DavidBriglio commented 1 year ago

I have merged this in, thank you for taking the time to contribute!