Skip to content

Commit

Permalink
[Messenger] add android
Browse files Browse the repository at this point in the history
  • Loading branch information
metajet98 committed Jan 15, 2022
1 parent 82fee92 commit f107774
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.MessageDialog;
import com.facebook.share.widget.ShareDialog;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;

Expand Down Expand Up @@ -45,6 +46,7 @@ public class FlutterShareMePlugin implements MethodCallHandler, FlutterPlugin, A
final private static String _methodWhatsAppPersonal = "whatsapp_personal";
final private static String _methodWhatsAppBusiness = "whatsapp_business_share";
final private static String _methodFaceBook = "facebook_share";
final private static String _methodMessenger = "messenger_share";
final private static String _methodTwitter = "twitter_share";
final private static String _methodSystemShare = "system_share";
final private static String _methodInstagramShare = "instagram_share";
Expand Down Expand Up @@ -97,6 +99,11 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
msg = call.argument("msg");
shareToFacebook(url, msg, result);
break;
case _methodMessenger:
url = call.argument("url");
msg = call.argument("msg");
shareToMessenger(url, msg, result);
break;
case _methodTwitter:
url = call.argument("url");
msg = call.argument("msg");
Expand Down Expand Up @@ -216,6 +223,43 @@ public void onError(FacebookException error) {

}

/**
* share to Messenger
*
* @param url String
* @param msg String
* @param result Result
*/
private void shareToMessenger(String url, String msg, Result result) {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(url))
.setQuote(msg)
.build();
MessageDialog shareDialog = new MessageDialog(activity);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
System.out.println("--------------------success");
}

@Override
public void onCancel() {
System.out.println("-----------------onCancel");
}

@Override
public void onError(FacebookException error) {
System.out.println("---------------onError");
}
});

if (shareDialog.canShow(content)) {
shareDialog.show(content);
result.success("success");
}
result.error("error", "Cannot share thought messenger", "");
}

/**
* share to whatsapp
*
Expand Down
16 changes: 16 additions & 0 deletions lib/flutter_share_me.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FlutterShareMe {
static const String _methodWhatsAppPersonal = 'whatsapp_personal';
static const String _methodWhatsAppBusiness = 'whatsapp_business_share';
static const String _methodFaceBook = 'facebook_share';
static const String _methodMessenger = 'messenger_share';
static const String _methodTwitter = 'twitter_share';
static const String _methodInstagramShare = 'instagram_share';
static const String _methodSystemShare = 'system_share';
Expand Down Expand Up @@ -116,6 +117,21 @@ class FlutterShareMe {
return result;
}

///share to messenger
Future<String?> shareToMessenger({required String msg, String url = ''}) async {
final Map<String, dynamic> arguments = <String, dynamic>{};
arguments.putIfAbsent('msg', () => msg);
arguments.putIfAbsent('url', () => url);
String? result;
try {
result = await _channel.invokeMethod<String?>(_methodMessenger, arguments);
} catch (e) {
return e.toString();
}
return result;
}


///share to twitter
///[msg] string that you want share.
Future<String?> shareToTwitter({required String msg, String url = ''}) async {
Expand Down

0 comments on commit f107774

Please sign in to comment.