Skip to content

Android 14+ Crash: PendingIntent with FLAG_MUTABLE causes app to crash on SDK 34+ #261

@khaledbk

Description

@khaledbk

Description:

When targeting Android 14 (SDK 34+), the app crashes due to usage of a PendingIntent with FLAG_MUTABLE and an implicit intent in RNBackgroundActionsTask.java. This behavior is restricted by the OS for security reasons.

To Reproduce:

Set compileSdkVersion = 34 and targetSdkVersion = 34
Use react-native-background-actions to run a background task

Observe crash logs:

java.lang.IllegalArgumentException: Targeting U+ disallows creating or retrieving a PendingIntent with FLAG_MUTABLE and an implicit Intent...

Problematic Code (Current Implementation):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
}

Proposed Fix:

Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
if (intent != null) {
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions