DavidBriglio / cordova-plugin-foreground-service

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

Application crashes on Android 11 - java.lang.SecurityException #19

Closed caiorsantanna closed 2 months ago

caiorsantanna commented 3 years ago

NOTE: Please make sure you read the readme and fully understand the use of this plugin and its requirements before opening an issue.

Describe the bug Hi, I've been working on the main app where I work. One of its main features is the ability to take pictures.

As you will see in the environment below, I use ionic with angular, and what happens to me is the following:

On some devices, the ionic camera plugin has been resetting its state, due to the memory wipe that android does with apps in the background, since the ionic camera plugin opens the camera's native app, and only after back to your app.

After some research I fixed this using the foreground plugin for ionic (this plugin), opening it before the camera starts, and then closing it, to prevent android from understanding that my app is in the background and resetting it.

This fixed the problem for most devices, except for some devices that use android 11, strangely not all.

After taking a look at the google play crash logs, I noticed that most of them gave the error below, coming from this plugin.

image image

To Reproduce Well, it's not very clear how to reproduce this problem. What I do know is that it happens on android 11 due to security issues implemented in this version, as you can see here: https://developer.android.com/guide/components/foreground-services

See some implementation of mine: image image

Expected behavior Should open the foreground service without crashing the app

Smartphone (please complete the following information):

About affected devices: image

About the version of things: image image

  "dependencies": {
    "@angular/common": "^7.2.2",
    "@angular/core": "^7.2.2",
    "@angular/forms": "^7.2.2",
    "@angular/platform-browser": "^7.2.2",
    "@angular/platform-browser-dynamic": "^7.2.2",
    "@angular/router": "^7.2.2",
    "@ionic-native/android-permissions": "^5.35.0",
    "@ionic-native/app-rate": "^5.12.0",
    "@ionic-native/app-version": "^5.21.5",
    "@ionic-native/barcode-scanner": "^5.0.0",
    "@ionic-native/call-number": "^5.1.0",
    "@ionic-native/camera": "^5.34.0",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/crop": "^5.0.0",
    "@ionic-native/device": "^5.0.0",
    "@ionic-native/file": "^5.0.0",
    "@ionic-native/file-transfer": "^5.28.0",
    "@ionic-native/foreground-service": "^5.34.0",
    "@ionic-native/image-picker": "^5.0.0",
    "@ionic-native/in-app-browser": "^5.15.1",
    "@ionic-native/ionic-webview": "^5.0.0",
    "@ionic-native/launch-navigator": "^5.0.0",
    "@ionic-native/market": "^5.21.5",
    "@ionic-native/media-capture": "^5.28.0",
    "@ionic-native/network": "^5.5.1",
    "@ionic-native/push": "^5.5.0",
    "@ionic-native/screen-orientation": "^5.2.0",
    "@ionic-native/social-sharing": "^5.35.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic-native/video-editor": "^5.28.0",
    "@ionic/angular": "^4.2.0",
    "@ionic/pro": "2.0.4",
    "@ionic/storage": "^2.2.0",
    "@ngx-translate/core": "^11.0.1",
    "@sentry/browser": "5.1.1",
    "@types/crypto-js": "^4.0.2",
    "@types/moment-timezone": "^0.5.12",
    "ajv": "^6.10.0",
    "angular2-signaturepad": "https://github.com/FieldControl/angular2-signaturepad",
    "br-mask": "0.0.10",
    "call-number": "1.0.1",
    "chart.js": "2.9.3",
    "cordova-android": "9.1.0",
    "cordova-android-support-gradle-release": "^2.1.0",
    "cordova-ios": "5.1.0",
    "cordova-plugin-actionsheet": "^2.3.3",
    "cordova-plugin-app-version": "^0.1.9",
    "cordova-plugin-apprate": "1.4.0",
    "cordova-plugin-background-fetch": "~7.0.1",
    "cordova-plugin-cocoalumberjack": "0.0.4",
    "cordova-plugin-crop": "^0.4.0",
    "cordova-plugin-device": "2.0.2",
    "cordova-plugin-dialogs": "^2.0.1",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-globalization": "1.11.0",
    "cordova-plugin-inappbrowser": "3.1.0",
    "cordova-plugin-ionic": "^5.3.0",
    "cordova-plugin-ionic-keyboard": "^2.1.3",
    "cordova-plugin-ionic-webview": "^2.4.1",
    "cordova-plugin-market": "^1.2.0",
    "cordova-plugin-media-capture": "^3.0.3",
    "cordova-plugin-nativestorage": "2.3.2",
    "cordova-plugin-network-information": "^2.0.1",
    "cordova-plugin-screen-orientation": "^3.0.1",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-telerik-imagepicker": "2.3.3",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova-sqlite-storage": "^3.2.0",
    "core-js": "^2.5.4",
    "crypto-js": "^4.1.1",
    "es6-promise-plugin": "^4.2.2",
    "ionic2-calendar": "^0.5.7",
    "leaflet": "^1.4.0",
    "lodash": "^4.17.11",
    "loki-cordova-fs-adapter": "^1.0.2",
    "lokijs": "^1.5.6",
    "moment-timezone": "^0.5.23",
    "phonegap-plugin-barcodescanner": "^8.0.1",
    "phonegap-plugin-multidex": "^1.0.0",
    "phonegap-plugin-push": "2.3.0",
    "rxjs": "^6.5.0",
    "semver": "^7.3.2",
    "uk.co.workingedge.phonegap.plugin.launchnavigator": "^5.0.4",
    "uuid": "^3.3.2",
    "zone.js": "~0.8.29"
  },
  "devDependencies": {
    "@angular-devkit/architect": "~0.12.3",
    "@angular-devkit/build-angular": "^0.13.8",
    "@angular-devkit/core": "~7.2.3",
    "@angular-devkit/schematics": "~7.2.3",
    "@angular/cli": "~7.2.3",
    "@angular/compiler": "~7.2.2",
    "@angular/compiler-cli": "^7.2.6",
    "@angular/language-service": "~7.2.2",
    "@ionic/angular-toolkit": "~1.2.0",
    "@types/chart.js": "^2.9.23",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/leaflet": "^1.4.2",
    "@types/lodash": "^4.14.120",
    "@types/lokijs": "^1.5.2",
    "@types/node": "~10.12.0",
    "@types/semver": "^7.3.4",
    "@types/uuid": "^3.4.4",
    "codelyzer": "~4.5.0",
    "cordova-background-geolocation": "git+https://github.com/transistorsoft/cordova-background-geolocation.git#4.0.1",
    "cordova-plugin-android-permissions": "^1.1.2",
    "cordova-plugin-androidx": "^3.0.0",
    "cordova-plugin-androidx-adapter": "^1.1.3",
    "cordova-plugin-camera": "^5.0.2",
    "cordova-plugin-foreground-service": "^1.1.3",
    "cordova-plugin-video-editor": "git+https://github.com/AlexMiniApps/cordova-plugin-video-editor.git",
    "cordova-plugin-x-socialsharing": "^6.0.3",
    "cordova-support-google-services": "^1.3.2",
    "fcm-node": "^1.4.0",
    "husky": "^4.2.5",
    "ionic": "5.4.16",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~8.0.0",
    "tslint": "~5.12.0",
    "typescript": "~3.1.6"
  }

Additional context

Well, i think the solution to this have some to do with this permissions: image

So i tried:

<service android:foregroundServiceType="camera" android:name="com.davidbriglio.foreground.ForegroundService.startPluginForegroundService" />

But when i tried to do it i am getting this error: image

And i am in a dead end :( What i doing wrong?

mirko77 commented 3 years ago

We are facing the same crashes on Android 10 as well. Also see #20, are you experiencing that as well?

caiorsantanna commented 3 years ago

We are facing the same crashes on Android 10 as well. Also see #20, are you experiencing that as well?

I think they are different errors, mine is about permissions and how the android 11 handle this. Your error is a null pointer exception, this is proprably something about the java code, try to use the latest version.

mirko77 commented 3 years ago

@caiorsantanna we are using 1.1.3. We know it is a different issue, we were asking whether your app is experiencing that error as well.

caiorsantanna commented 3 years ago

@caiorsantanna we are using 1.1.3. We know it is a different issue, we were asking whether your app is experiencing that error as well.

Sorry, yes, we have this error aswell: image

mirko77 commented 2 years ago

@caiorsantanna we are currently testing this fork, you could give it a try https://bitbucket.org/epicollect/cordova-foreground-plugin/src/master/

LillyWebsites commented 2 years ago

Same for me, mostly on Redmi devices with Android 11:

java.lang.RuntimeException: at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:4446) at android.app.ActivityThread.access$2000 (ActivityThread.java:257) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2020) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loop (Looper.java:236) at android.app.ActivityThread.main (ActivityThread.java:8059) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:656) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:967) Caused by: java.lang.SecurityException: at android.os.Parcel.createExceptionOrNull (Parcel.java:2376) at android.os.Parcel.createException (Parcel.java:2360) at android.os.Parcel.readException (Parcel.java:2343) at android.os.Parcel.readException (Parcel.java:2285) at android.app.INotificationManager$Stub$Proxy.deleteNotificationChannel (INotificationManager.java:4038) at android.app.NotificationManager.deleteNotificationChannel (NotificationManager.java:909) 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:4423) Caused by: android.os.RemoteException: at com.android.server.notification.NotificationManagerService$10.enforceDeletingChannelHasNoFgService (NotificationManagerService.java:3436) at com.android.server.notification.NotificationManagerService$10.deleteNotificationChannel (NotificationManagerService.java:3449) at android.app.INotificationManager$Stub.onTransact (INotificationManager.java:1737) at android.os.Binder.execTransactInternal (Binder.java:1157) at android.os.Binder.execTransact (Binder.java:1126)

caiorsantanna commented 2 months ago

We migrate to Capacitor

https://github.com/capawesome-team/capacitor-plugins/tree/main/packages/android-foreground-service

mirko77 commented 2 months ago

@caiorsantanna

Thank you for your message. However, the current repository is not fully open source. To access it, one must be a sponsor, as detailed at Capawesome Insiders.

Our policy is to distribute our code as open source, and using a "sponsorship" based plugin would require any forks to also become sponsors. This would not align with our open-source distribution model.