react-nativediscordsharedeep-linking

React-native share on discord


I'm implementing a feature on a project where we want users to be able to share a post on discord. Implementing sharing on facebook or twitter was easy using https://react-native-community.github.io/react-native-share/docs/share-single

But there's no support for discord on shareSingle and I don't want a share button calling for Native Share menu which could link to discord, I want a discord icon redirecting directly to discord. But I can't even find a way to open discord app with url schemas or universal link to trigger the app (discord:// or discord://discord or discordapp:// ...).

I tried to find a way to get access to native sharing in order to open discord sharing singly, but couldn't find a way.

Does anyone implemented discord sharing and have any tips on how to achieve that in react-native ?


Solution

  • You can make a fork the project and add this files:

    android/src/main/java/cl/json/social/DiscordShare.java

    package cl.json.social;
    
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import java.io.File;
    import android.os.Environment;
    import android.net.Uri;
    
    import com.facebook.react.bridge.ReactApplicationContext;
    import com.facebook.react.bridge.ReadableMap;
    
    
    public class DiscordShare extends SingleShareIntent {
    
        private static final String PACKAGE = "com.discord";
        private static final String PLAY_STORE_LINK = "https://play.google.com/store/apps/details?id=com.discord";
    
        public DiscordShare(ReactApplicationContext reactContext) {
            super(reactContext);
        }
    
        @Override
        public void open(ReadableMap options) throws ActivityNotFoundException {
            super.open(options);
            //  extra params here
            this.openIntentChooser();
        }
    
        @Override
        protected String getPackage() {
            return PACKAGE;
        }
    
        @Override
        protected String getDefaultWebLink() {
            return null;
        }
    
        @Override
        protected String getPlayStoreLink() {
            return PLAY_STORE_LINK;
        }
    }
    

    Edit android/src/main/java/cl/json/RNShareModule.java

    package cl.json;
    // ...
    
    import cl.json.social.DiscordShare;
    public class RNShareModule extends ReactContextBaseJavaModule implements ActivityEventListener {
     // ...
    private enum SHARES {
            facebook,
            generic,
            pagesmanager,
            twitter,
            whatsapp,
            instagram,
            instagramstories,
            googleplus,
            email,
            pinterest,
            messenger,
            snapchat,
            sms,
            linkedin,
            discord; // ADDED
    
    
            public static ShareIntent getShareClass(String social, ReactApplicationContext reactContext) {
                SHARES share = valueOf(social);
                switch (share) {
                    case generic:
                        return new GenericShare(reactContext);
                    case facebook:
                        return new FacebookShare(reactContext);
                    case pagesmanager:
                        return new FacebookPagesManagerShare(reactContext);
                    case twitter:
                        return new TwitterShare(reactContext);
                    case whatsapp:
                        return new WhatsAppShare(reactContext);
                    case instagram:
                        return new InstagramShare(reactContext);
                    case instagramstories:
                        return new InstagramStoriesShare(reactContext);
                    case googleplus:
                        return new GooglePlusShare(reactContext);
                    case email:
                        return new EmailShare(reactContext);
                    case pinterest:
                        return new PinterestShare(reactContext);
                    case sms:
                        return new SMSShare(reactContext);
                    case snapchat:
                        return new SnapChatShare(reactContext);
                    case messenger:
                        return new MessengerShare(reactContext);
                    case linkedin:
                        return new LinkedinShare(reactContext);
                    case discord:
                        return new DiscordShare(reactContext); // ADDED
                    default:
                        return null;
                }
            }
        };
    
    }
    // ...
    

    Finally edit index.js

    //...
    class RNShare {
    //...
    static Social = {
        FACEBOOK: NativeModules.RNShare.FACEBOOK || 'facebook',
        FACEBOOK_STORIES: NativeModules.RNShare.FACEBOOK_STORIES || 'facebook-stories',
        PAGESMANAGER: NativeModules.RNShare.PAGESMANAGER || 'pagesmanager',
        TWITTER: NativeModules.RNShare.TWITTER || 'twitter',
        WHATSAPP: NativeModules.RNShare.WHATSAPP || 'whatsapp',
        INSTAGRAM: NativeModules.RNShare.INSTAGRAM || 'instagram',
        INSTAGRAM_STORIES: NativeModules.RNShare.INSTAGRAM_STORIES || 'instagramstories',
        GOOGLEPLUS: NativeModules.RNShare.GOOGLEPLUS || 'googleplus',
        EMAIL: NativeModules.RNShare.EMAIL || 'email',
        PINTEREST: NativeModules.RNShare.PINTEREST || 'pinterest',
        LINKEDIN: NativeModules.RNShare.LINKEDIN || 'linkedin',
        SMS: NativeModules.RNShare.SMS || 'sms',
        DISCORD: NativeModules.RNShare.DISCORD || 'discord', // ADDED
      };
    // ...
    }
    
    

    In your project use:

        Share.shareSingle({
          title: 'test',
          message: 'hola mundo',
          url: 'https://json.cl',
          social: Share.Social.DISCORD,
        })
          .then((res) => {
            console.log(res);
          })
          .catch((err) => {
            err && console.log(err);
          });
    

    Result:

    Result Discord Share