Facebook send message (MessageDialog) is not working - android

I am really wasting time on trying to figure out what might be wrong. I have a button(not fb send button) and on click i want to open messenger(or with any other way) to send a message to users. This is my code:
MessageDialog messageDialog = new MessageDialog(thisFragment);
if (MessageDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
messageDialog.show(linkContent);
}else{
Log.e(TAG, "Can't be shown");
}
I tried as many code sample as i could find on the internet. The facebook documentation on sharing is really poor.The code always executes the 'else' block on my if statement.
I can show the ShareDialog with the same exact procedure. But MessageDialog is not working and it doens't even logs anything. Is there any way to send a message? Or any source that is tested and working?
Thanks a lot

Related

Xamarin Forms: Android CustomTabs close when app is backgrounded

TL;DR: CustomTabs close when app is backgrounded, but we need it to stay active. How can we achieve this?
We have an app which uses CustomTabs to login the user. We have added two-factor authentication, but this introduces a problem. When you tap the login button the custom tabs intent is launched as this:
var tcs = new TaskCompletionSource<BrowserResult>();
try
{
var activity = (Activity)Forms.Context;
var builder = new CustomTabsIntent.Builder().EnableUrlBarHiding();
var customTabsIntent = builder.Build();
customTabsIntent.Intent.AddFlags(ActivityFlags.NoHistory);
Action<string> callback = null;
callback = url =>
{
MainActivity.Callbacks -= callback;
tcs.SetResult(new BrowserResult
{
ResultType = BrowserResultType.Success,
Response = url
});
};
MainActivity.Callbacks += callback;
customTabsIntent.LaunchUrl(activity, Android.Net.Uri.Parse(options.StartUrl));
}
catch (Exception ex)
{
throw new Exception($"error: {ex.Message}");
}
return tcs.Task;
This works as expected and you can login, of course now the code for two-factor authentication code is asked, which in most cases means you have to background the app, open your authenticator (authy, Google authenticator etc.) and then come back to the app with the code. The problem is that when we re-open the app the CustomTabs and it's session is completely gone. This means you have to click it again, login again and the same happens all over. I have searched for a solution for days now. Can anyone help us finding a way to keep the CustomTabs and it's session open, so you can just fill in your authenticator code and login happily ever after?
Okay it seems to be that the following code that someone added was the one causing this:
customTabsIntent.Intent.AddFlags(ActivityFlags.NoHistory);
I removed it and it seems to work now. I see some other issues going on, but I don't think this has to do with this issue. If it does I will let you know here.

Getting "Content not found" error when following app-scoped user ID

Thanks again to Cbroe who answered my original question. I implemented his suggestion and although the URL returned by GraphRequest.newMeRequest looks valid, when I followed it, I got a "Content not found" error:
reachOut.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
// Navigate to person's Facebook page to send a message.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkArray[selectedPosition]));
startActivity(intent);
}
});
BTW, in the above, I did confirm that linkArray[selectedPosition] contained the correct value.
I had previously (back in 2018!) successfully completed an app review for user_link, so I doubt that that's the problem.
Looks like I had to resubmit my app for approval because now it's working again.

How to handle Facebook ShareDialog.canShow() == false on Android

Facebook's Sharing on Android documentation tells us to show the ShareDialog with the following code snippet:
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(
"The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent);
}
My question is why do we need to check ShareDialog.canShow()? In what scenarios would this possibly return false and do we need to handle this scenario? The example code would just fail silently not tell the user anything.
I spent a lot of time yesterday trying to debug an error related to this. Facebook's docs are poor in this sense and when there is an error related to this canShow() method, it just fails silently, no logging at all.
So, to answer your question:
My question is why do we need to check ShareDialog.canShow()? In what
scenarios would this possibly return false and do we need to handle
this scenario?
Based on the scenario I faced: when the user doesn't have the Facebook app installed on their device and you want to share photos (SharePhotoContent) or videos (ShareVideoContent), canShow() will return false. The reason is that Facebook SDK's WebView version doesn't support sharing this kind of content.
I found this out debugging their FacebookActivity class, on the handlePassThroughError() method. The (not logged) error message is:
"Unable to show the provided content. This typically means that the Facebook app is not installed or up to date. If showing via the Web, this could mean that the content has properties that are not supported via this channel."
So, what should we do when canShow() returns false?
It depends on your scenario. Possible solutions would be:
Show a dialog (or SnackBar) telling the user that they need to install the Facebook app to be able to share this kind of content;
Request an authentication token, log the user using the Facebook SDK, and share the content using your own dialog and calling Facebook API directly.
Possible solutions for Facebook would be include this on their documentation or log this error on LogCat.
Hope it helps!
this answer for someone facing the same problem.
#leocadiotine was great.
From the facebook sdk sample, when the ShareDialog.canShow() return false you should use ShareApi.share function.
In AndroidManifest.xml, add:
<queries>
<provider android:authorities="com.facebook.katana.provider.PlatformProvider" />
</queries>
// for facebook
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});

WebDialog.RequestsDialogBuilder does not actually send a request?

I am working on an app, which sends a request to the selected user. Am selecting the user through FriendPickerFragment(Android.Support.V4.App). I am successfully getting the list of friends I have selected on the "Done" button of the Picker fragment. Post that, I am trying to build a request dialog, which should not show up in my app, but should send the FB request to the selected friends. Here is my code:
Bundle bundle = new Bundle ();
bundle.PutString ("app_id", AppID);
var dialog = new WebDialog.RequestsDialogBuilder (this.Activity, Session.ActiveSession, bundle).SetMessage(user + " has invited you to join my application");
dialog.Build ();
The problem I am facing is that, the request is not actually sent. There is no exception either. I even implemented the call back:
public void OnComplete(Bundle bundle, FacebookException e)
but the execution does NOT go in the callback. What am I missing here ? Any help would be greatly appreciated.
Tried dialog.SetTo(userId) before build also....still not working...
Any suggestions ?
Well, according to the documentation for RequestDialogBuilder the method public WebDialog build(), the behavior you describe is by design. Quote:
"The dialog is not shown, but is ready to be shown by calling Dialog.show()"
It sounds like you do not want to show a dialog, but just want to send a request without any UI. You are using the wrong tool to do that. To send a request using a dialog, you have to show the dialog, and the user action is what causes a request to be sent. This doesn't require any special permission to be granted ahead of time, because it only sends a request if the user chooses to after your app opens the dialog.
There may or may not be a way to do what you want (send an app request without any user interaction) ... if there is a way, it will be along these lines:
use the Graph API, get a token with some permission that the user grants to your app. Here's a place to start looking:
https://developers.facebook.com/docs/graph-api/reference/v2.1/user/apprequests/

Android Facebook SDK how to post a simple status update to my wall (just text, no link)

I have a text in my android application and I want the user to be able to click the FB Share button in the app and get this text posted on their own wall.
So far I've been following this tutorial:
https://developers.facebook.com/docs/android/share
and it is only good for either sharing a link (I dont want to share a link) or bringing up the share status dialog, but it is empty.
how can I populate that dialog with the text I want to populate it with?
this is my code
private void shareOnFacebook(String textToBeShared) {
if (FacebookDialog.canPresentShareDialog(a, FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
// Publish the post using the Share Dialog
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(a).setCaption(textToBeShared).build();
a.uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
publishFeedDialog();
}
} // End of shareOnFacebook
I tried with setLink(null).setDescription, setName, setCaption but none of those worked.
The short answer is, you can't.
Prefilling the text is against Platform policies (see section 2.3 of https://developers.facebook.com/policy/), and as such, the Android SDK doesn't allow you to do it.

Categories

Resources