feat(Android): allow setting foregroundServiceType (+ improve stability)#265
feat(Android): allow setting foregroundServiceType (+ improve stability)#265GaelCO wants to merge 13 commits intoRapsssito:masterfrom
foregroundServiceType (+ improve stability)#265Conversation
|
My PR is intended to address the following issues: |
|
Hi @Rapsssito , |
There was a problem hiding this comment.
Please, remove the changes to .d.ts files, these files are autogenerated on release based on JSDocs.
|
Thanks @GaelCO for the PR! Please, see my comments. |
foregroundServiceType (+ improve stability)
There was a problem hiding this comment.
Pull request overview
This PR aims to improve Android foreground-service stability (Android 12+) and add support for configuring the foreground service type from JS, with updated documentation and TypeScript types.
Changes:
- Add
foregroundServiceTypeto JS options normalization and TypeScript declarations. - Switch Android service start to
ContextCompat.startForegroundServiceandServiceCompat.startForeground, plus handle null intents and Android 14+onTimeout. - Document the new option and required Android manifest/permission setup.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/index.js | Adds foregroundServiceType to the JS options contract and passes it through normalization. |
| lib/types/index.d.ts | Extends TS types to include foregroundServiceType in public API typings. |
| android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java | Uses ServiceCompat.startForeground, handles null intents, and stops service on onTimeout. |
| android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java | Maps JS foregroundServiceType string to Android ServiceInfo.FOREGROUND_SERVICE_TYPE_* constants. |
| android/src/main/java/com/asterinet/react/bgactions/BackgroundActionsModule.java | Starts service via ContextCompat.startForegroundService and clears intent on stop. |
| README.md | Documents foregroundServiceType and updates the options table. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java
Show resolved
Hide resolved
|
Hi, I reverted changes in d.ts . |
|
Hi @Rapsssito, |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java
Outdated
Show resolved
Hide resolved
android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
Outdated
Show resolved
Hide resolved
|
The co-pilot requests have been processed |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java:92
onStartCommandthrows whenintent.getExtras()is null, which can crash the process and undermines the stated stability improvements (e.g., service restarts or mis-starts). Consider handlingextras == nulllikeintent == null:stopSelf()and returnSTART_NOT_STICKY(or otherwise fail gracefully) instead of throwing.
final Bundle extras = intent.getExtras();
if (extras == null) {
throw new IllegalArgumentException("Extras cannot be null");
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| | Value | Android API | `<uses-permission>` required | | ||
| | --- |-------------| --- | | ||
| | `"dataSync"` *(default)* | 29 (Q) | `android.permission.FOREGROUND_SERVICE_DATA_SYNC` | | ||
| | `"mediaPlayback"` | 29 (Q) | `android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK` | | ||
| | `"phoneCall"` | 29 (Q) | `android.permission.FOREGROUND_SERVICE_PHONE_CALL` | |
There was a problem hiding this comment.
Docs say "dataSync" is the default foreground service type, but the implementation returns 0 when foregroundServiceType is omitted (no default is applied). Either implement the stated default (e.g., treat undefined/empty as dataSync on API 29+) or adjust the documentation to avoid implying a default that isn’t enforced.
| for (final String type : types) { | ||
| final int flag = typeToForegroundServiceFlag(type); | ||
| if (flag != 0) { | ||
| result |= flag; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private int typeToForegroundServiceFlag(@NonNull final String type) { | ||
| switch (type) { | ||
| case "dataSync": |
There was a problem hiding this comment.
getForegroundServiceType() iterates types and passes each entry to typeToForegroundServiceFlag(type). If the JS array contains null (or a non-string that ends up as null in the Bundle), switch (type) will throw a NullPointerException and crash the service. Consider filtering/validating entries (e.g., skip null/empty values) before the switch so invalid input can’t crash the app process.
-> use ServiceCompat
-> allow setting foregroundServiceType
-> Improve stability for android 12+
-> manage service onTimeOut to stop service on android 14+