Skip to content

feat(Android): allow setting foregroundServiceType (+ improve stability)#265

Open
GaelCO wants to merge 13 commits intoRapsssito:masterfrom
GaelCO:master
Open

feat(Android): allow setting foregroundServiceType (+ improve stability)#265
GaelCO wants to merge 13 commits intoRapsssito:masterfrom
GaelCO:master

Conversation

@GaelCO
Copy link
Copy Markdown

@GaelCO GaelCO commented Mar 23, 2026

-> use ServiceCompat
-> allow setting foregroundServiceType
-> Improve stability for android 12+
-> manage service onTimeOut to stop service on android 14+

@GaelCO GaelCO changed the title Improve stability Improve stability + allow pass foregroundServiceType Mar 25, 2026
@GaelCO GaelCO changed the title Improve stability + allow pass foregroundServiceType Improve stability + allow setting foregroundServiceType Mar 25, 2026
@GaelCO
Copy link
Copy Markdown
Author

GaelCO commented Mar 25, 2026

@GaelCO
Copy link
Copy Markdown
Author

GaelCO commented Mar 25, 2026

Hi @Rapsssito ,
Is this project still maintained? I opened a PR to improve react-native-background-actions and would be happy to help maintain if needed

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please, remove the changes to .d.ts files, these files are autogenerated on release based on JSDocs.

@Rapsssito
Copy link
Copy Markdown
Owner

Thanks @GaelCO for the PR! Please, see my comments.

@Rapsssito Rapsssito requested a review from Copilot March 25, 2026 12:00
@Rapsssito Rapsssito changed the title Improve stability + allow setting foregroundServiceType feat(Android): allow setting foregroundServiceType (+ improve stability) Mar 25, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 foregroundServiceType to JS options normalization and TypeScript declarations.
  • Switch Android service start to ContextCompat.startForegroundService and ServiceCompat.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.

@GaelCO
Copy link
Copy Markdown
Author

GaelCO commented Mar 25, 2026

Hi, I reverted changes in d.ts .
Would you like me to incorporate Copilot’s suggestions?

@GaelCO
Copy link
Copy Markdown
Author

GaelCO commented Mar 26, 2026

Hi @Rapsssito,
I modified the signature to allow multiple foregroundServiceType values. I considered allowing both a string and an Array, but I thought that would be less clear, so I only allowed Array.
I took Copilot’s feedback into account, but since I made quite a few changes, it might be worth running it through again.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

@GaelCO
Copy link
Copy Markdown
Author

GaelCO commented Mar 30, 2026

The co-pilot requests have been processed

@Rapsssito Rapsssito requested a review from Copilot March 30, 2026 19:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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

  • onStartCommand throws when intent.getExtras() is null, which can crash the process and undermines the stated stability improvements (e.g., service restarts or mis-starts). Consider handling extras == null like intent == null: stopSelf() and return START_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.

Comment on lines +137 to +141
| 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` |
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +118 to +129
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":
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants