react-native-share / react-native-share

Social share, sending simple data to other apps.

Home Page:https://react-native-share.github.io/react-native-share

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

vaish8529 opened this issue · comments

I'm using the react-native-share package to share a component as an image. It works fine on Android versions below 33, but on Android 33, it throws an error: 'Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a Pending Intent.' How can I fix this issue?

After updating react-native-share package its says,

react-native-share compile SDK Version is not specified. Please add it to build.gradle.

What went wrong: A problem occurred evaluating project ':react-native-share'.
Could not set unknown property 'namespace' for extension 'android' of type com.android.build.gradle.LibraryExtension.

buildToolsVersion = "33.0.2"
minSdkVersion = 21
compileSdkVersion = 33
targetSdkVersion = 33
On my app/build.gradle file inside dependencies

implementation 'androidx.work:work-runtime:2.7.1'

Same issue
"react-native": "0.62.2", "react-native-share": "^3.3.2"

Same issue

I fixed the issue by adding the following code to the AndroidManifest.xml file.

<queries>
        <package android:name="com.example.store" />
        <package android:name="com.example.services" />

        <!--for example, to share via instagram -->
        <package android:name="com.instagram.android" />
        <package android:name="com.whatsapp.android" />
        <intent>
          <action android:name="android.intent.action.SEND" />
          <data android:mimeType="image/jpeg" />
        </intent>
    </queries>

And

const shareOptions = {
            url: uri,
            social: Share.Social.WHATSAPP,
        };
    Share.shareSingle(shareOptions);

But it only shares on WhatsApp.
The error persists when using the Share.open() function.

any updates? how to fix this issue??

Same issue

Found a fix, you have to go into react-native-share/android/src/main/java/cl/json/social/TargetChosenReceiver.java and change to the code below. Then use a library like patch-package to save the patch so it works on every npm install...

This...

        Intent intent = new Intent(sTargetChosenReceiveAction);
        intent.setPackage(reactContext.getPackageName());
        intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());
        final PendingIntent callback = PendingIntent.getBroadcast(reactContext, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);```

To...

        Intent intent = new Intent(sTargetChosenReceiveAction);
        intent.setPackage(reactContext.getPackageName());
        intent.setClass(reactContext, TargetChosenReceiver.class);
        intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode());

        final PendingIntent callback;
        if (android.os.Build.VERSION.SDK_INT >= 23) {
            callback = PendingIntent.getBroadcast(reactContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        } else {
            callback = PendingIntent.getBroadcast(reactContext, 0, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
        }

@yaeda that also fixes it with better code, thank you!

But, I am using an older version of react-native-share and cannot update at the moment so need to do it in a patch.

I think this problem has been fixed in v7.0.1.

https://github.com/react-native-share/react-native-share/blob/main/android/src/main/java/cl/json/social/TargetChosenReceiver.java#L56-L59

Or is there some other problem?

Thanks @yaeda Fixed here

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and i will leave this open