Social Sharing feature

Hello,

What I am trying to do is to share some text with some of the available/installed social apps, like twitter, facebook, whatsapp, linkedin,…

A popup should be displayed, and the user should choose which app is he going to share with this text. it is commonly use on many applications.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

This is the code that should be called on a normal Android application, but how to integrate it within cocos2d-JS?, i tried to add it to the appActivity.java and call it using JSB reflection with no luck.

Did someone tried something similar using cocos2d-js?

Is that much difficult to integrate such simple feature with cocos2d!!!

I think the way to go is the jsb reflection that you mention. Maybe we could focus on which was your problem with that.

@AlexXx Thanks for your response.
Did you tried to integrate it with jsb reflection before? did you succeeded?
I tried to implement it like i normaly do and it was supposed to be an easy task, however after a succesfull compile nothing happens when i call this method using jsb reflection like i normaly do. and the weird thing is that no error messages were reproduced as if everything is working well!!.

    List<String> mList=new ArrayList<String>();
        mList.add("com.facebook.katana");
        mList.add("com.twitter.android");
        //Do not include GMAIL
        //mList.add("com.google.android.gm");
        
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
        List<ResolveInfo> resInfo = Cocos2dxActivity.getContext().getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()){
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
                targetedShare.setType("text/plain"); // put here your mime type
                if (mList.contains(info.activityInfo.packageName.toLowerCase())) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT, "");
                    targetedShare.putExtra(Intent.EXTRA_TEXT, textToShare);
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                    mList.remove(info.activityInfo.packageName.toLowerCase());
                }
            }
            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
            Cocos2dxActivity.getContext().startActivity(chooserIntent);
        }
    }

We do this, but then you have to add each app you want. Maybe there’re better solutions out there. Maybe there is some post in stackoverflow or something if our solution is not what you are searching for.

Thanks! @alexgg but should i insert this code into frameworks\runtime-src\proj.android\src\org\cocos2dx\javascript or where?

Is that compatible cocos2d-js or cocos2d-x?

I have it in my MainActivity.java in the android project, then I call the function with jsb reflection from the js code.

Have you tried using Cocos Helper http://sonarsystems.co.uk/cocoshelper.php

Thanks for the suggestions @alexgg @SonarSystems ill try them and update this thread, meanwhile any additional suggestion is really appreciated.

1 Like

Your very welcome, as we are the creators of the Cocos Helper feel free to ask any questions you may have.

If you use cocos helper I am sure it’s better than our implementation of sharing so I’d go for cocos helper too. @SonarSystems are amazing ;), sure they’ll help you if you have any doubt configuring it.
But use whatever you see fits better if you have any further doubt just ask.

1 Like

@alexgg @SonarSystems now I have multiple solutions and even if one of them works I will try the other one for learning purpose :smiley:.

Btw i have another opened thread, I don’t know if it is allowed to promote it here, but please take a look at it if anyone of you could help in this regards ( regarding cocos2d-js code obfuscation).

Thanks for your Help, really appreaciated :wink:

Sorry guys @alexgg @SonarSystems , I’ve spent 3 days working, compiling and trying different versions of cocos2d-JS, and at the end I discovered that it the issue is that I forgot to add the “static” keyword to my function declaration :worried:

Now it is working fine :smile: and that this could help someone committing the same mistake.

btw, thank you @SonarSystems for your great tutorials and videos.

1 Like

Your very welcome, simple mistakes happen to the best of us. I was halted by a missing new keyword a couple of weeks ago LOL