Skip to content

Commit

Permalink
Update ForegroundService.java
Browse files Browse the repository at this point in the history
  • Loading branch information
brunochikuji authored Jun 25, 2024
1 parent 4604957 commit 6582046
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void keepAwake()

if (!isSilent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIFICATION_ID, makeNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
startForeground(NOTIFICATION_ID, makeNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
startForeground(NOTIFICATION_ID, makeNotification());
}
Expand Down

3 comments on commit 6582046

@lmarcelocc
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

@brunochikuji , why FOREGROUND_SERVICE_TYPE_DATA_SYNC and not FOREGROUND_SERVICE_TYPE_LOCATION ?

Thanks :)

@brunochikuji
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lmarcelocc , it was actually an implementation made following a suggestion from another client who was using the lib.

FOREGROUND_SERVICE_TYPE_LOCATION makes sense and I'll test it by changing it

@lmarcelocc
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @brunochikuji ,

If I changed to FOREGROUND_SERVICE_TYPE_LOCATION I'm having this issue testing on Android 14. It happens only when user did not granted permissions and cordova-plugin-background-mode calls keepAwake() -> startForeground(). If user has alredy granted permissions, it works. So it seems we need to guarantee that we've all the permissions before calling the keepAwake() method.

My first though was to subscribe to those permissions and only call keepAwake() when granted.
My knowledge here is limited, but will keep this post update with my findings.

FATAL EXCEPTION: main
Process: io.gaf.map, PID: 17439
java.lang.RuntimeException: Unable to create service de.appplant.cordova.plugin.background.ForegroundService: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{e8f9104 17439:io.gaf.map/u0a389} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION]  and the app must be in the eligible state/exemptions to access the foreground only permission
at android.app.ActivityThread.handleCreateService(ActivityThread.java:5111)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2506)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:230)
at android.os.Looper.loop(Looper.java:319)
at android.app.ActivityThread.main(ActivityThread.java:8919)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Caused by: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{e8f9104 17439:io.gaf.map/u0a389} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION]  and the app must be in the eligible state/exemptions to access the foreground only permission
at android.os.Parcel.createExceptionOrNull(Parcel.java:3069)
at android.os.Parcel.createException(Parcel.java:3053)
at android.os.Parcel.readException(Parcel.java:3036)
at android.os.Parcel.readException(Parcel.java:2978)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7234)
at android.app.Service.startForeground(Service.java:862)
at de.appplant.cordova.plugin.background.ForegroundService.keepAwake(ForegroundService.java:134)
at de.appplant.cordova.plugin.background.ForegroundService.onCreate(ForegroundService.java:101)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:5098)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2506at android.os.Handler.dispatchMessage(Handler.java:106at android.os.Looper.loopOnce(Looper.java:230at android.os.Looper.loop(Looper.java:319at android.app.ActivityThread.main(ActivityThread.java:8919at java.lang.reflect.Method.invoke(Native Methodat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103) `

Please sign in to comment.