JulianAssmann / flutter_background

A flutter plugin to keep apps running in the background via foreground services. Android only.
https://pub.dev/packages/flutter_background
MIT License
89 stars 52 forks source link

Unable to create service java.lang.IllegalArgumentException: Invalid importance level #37

Closed Alt791350 closed 3 years ago

Alt791350 commented 3 years ago

I was testing some of my code and noticed this error when initializing for the very first time (when you just installed the app and initializing for the first time).

The full error:

java.lang.RuntimeException: Unable to create service de.julianassmann.flutter_background.IsolateHolderService: java.lang.IllegalArgumentException: Invalid importance level
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
    at android.app.ActivityThread.access$1300(ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: Invalid importance level
    at android.os.Parcel.createException(Parcel.java:1954)
    at android.os.Parcel.readException(Parcel.java:1918)
    at android.os.Parcel.readException(Parcel.java:1868)
    at android.app.INotificationManager$Stub$Proxy.createNotificationChannels(INotificationManager.java:1751)
    at android.app.NotificationManager.createNotificationChannels(NotificationManager.java:541)
    at android.app.NotificationManager.createNotificationChannel(NotificationManager.java:529)
    at de.julianassmann.flutter_background.IsolateHolderService.onCreate(IsolateHolderService.kt:63)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
    ... 8 more
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.notification.RankingHelper.createNotificationChannel(RankingHelper.java:659)
    at com.android.server.notification.NotificationManagerService$10.createNotificationChannelsImpl(NotificationManagerService.java:2227)
    at com.android.server.notification.NotificationManagerService$10.createNotificationChannels(NotificationManagerService.java:2242)
    at android.app.INotificationManager$Stub.onTransact(INotificationManager.java:256)
    at android.os.Binder.execTransact(Binder.java:731)

So I wrote some test code to make sure it is just this package and not a conflict with something or other code I wrote wrong.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (FlutterBackground.isBackgroundExecutionEnabled == false) {
    if (await FlutterBackground.initialize(
          androidConfig: FlutterBackgroundAndroidConfig(
            notificationImportance: AndroidNotificationImportance.Low,
            notificationTitle: "Background Service",
            notificationText: "Running in the background",
          ),
        ) ==
        true) await FlutterBackground.enableBackgroundExecution();
  }
  runApp(MyApp());
}

AndroidNotificationImportance.Low and AndroidNotificationImportance.Min throw both Invalid importance level. The error gets thrown when await FlutterBackground.enableBackgroundExecution(); is executed when the permissions gets accepted for the first time. Starting the app with the permission already accepted throws no error for any AndroidNotificationImportance level.

JulianAssmann commented 3 years ago

I was able to reproduce the error message. It seems that only PRIORITY_DEFAULT, PRIORITY_HIGH and PRIORITY_MAX are valid priority levels when creating the notification channel for a foreground service notification, even though the documentation claims otherwise 🤔 I think the fix would be to just remove the two invalid options. What do you think?

Nuyoahhjh commented 2 months ago

Can this notice be hidden