cordova-plugin-background-mode: App crashes on android 9.0

Issue description

My app crashes on android 9.0(My phone is Xiaomi 8), it works well on earlier platform(Android 6.0, 7.0, 8.0).

It crashes when the screen turns off or I switch to another app.

Error log from Android Studio

Similar to https://github.com/katzer/cordova-plugin-background-mode/issues/393#issue-346518462

2019-02-21 21:08:28.938 6320-6320/cc.cs_tao.whu_library_seat_mobile E/AndroidRuntime: FATAL EXCEPTION: main
    Process: cc.cs_tao.whu_library_seat_mobile, PID: 6320
    android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=-2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1760)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

My project info


Can anyone help me? Many thanks.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 22

Commits related to this issue

Most upvoted comments

@markarupert

I am having a problem on Android 9 as well when I pause the app.

As @CS-Tao mentioned this works for me also: cordova.plugins.backgroundMode.setDefaults({ silent: true });

What is the ramification for my app to setting silent true? Is it still going to work in the back ground?

From the app plugin:

In silent mode the plugin will not display a notification - which is not the default. Be aware that Android recommends adding a notification otherwise the OS may pause the app.

I was looking for another solution and found that:

You can also add the permission to Ionic 3 / Cordova apps by adding this in your config.xml under android platform:

    <platform name="android">
        <config-file parent="./" target="app/src/main/AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
        </config-file>
    </platform>

It fixed my crash issue. Now just need to test if android 9 leaves the app running in the background or it kills it after 5 minutes.

So I just found out that when the targetSdkVersion is set to 27, the plugin works perfectly well but when changing it to 28 the issue is back, and can only be solved by actively adding the FOREGROUND_SERVICE permission.

I think this issue should be reopened and the permission should be included in the plugin itself.

It will not crash if I do not show the notification from this plugin.

cordova.plugins.backgroundMode.setDefaults({ silent: true })

Note: Notifications from cordova-plugin-local-notification still work well in my app.

@CS-Tao @KinG-InFeT for me this solution it’s working:

cordova plugin rm cordova-plugin-background-mode
cordova plugin add https://github.com/katzer/cordova-plugin-background-mode --nofetch

because the external package contains these changes https://github.com/katzer/cordova-plugin-background-mode/pull/389

@Genarito : ok sir I will try another solutiona and let u know it’s working or not.

@weglov Thank you, It works!

And @Genarito ‘s solutions can solve this problem, too. I’m sorry that I did not delete the caches in Travis-ci before, so Travis always releases the apk with the old plugin.

Try this:

cordova plugin add https://github.com/katzer/cordova-plugin-background-mode --no-fetch
ionic cordova platform remove android
ionic cordova platform add android

That fixed the problem for me. And It fixed an iOS bug too where app didn’t start even.

Hope It helps