Here's the flow I am trying to make work:
So, for this solution I am using both RestFB (http://restfb.com/) and Facebook Android API v3.0
When user clicks the Like button, I do the following:
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
//facebookClient is the RestFB object
facebookClient = new DefaultFacebookClient(session.getAccessToken());
Intent i = new Intent(MainActivity.this, FacebookWebViewActivity.class);
startActivity(i);
}
};
With this code I can successfully log into Facebook (get the AUTH_CODE) and then I invoke my WebView, which should direct the user to the Fan Page I want. This WebView activity is very simple and simply do:
webView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
Intent intent = new Intent(FacebookWebViewActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
return false;
}
});
webView.loadUrl("https://www.facebook.com/<MyFanPage>");
If user presses back button while in the WebView, I should redirect him to my MainActivity. I was expecting the activity's onResume()
method to trigger when this happens, but it's not, for some reason.... I intend to put the checkIfLikes(null)
on this method to check if the liked happened or not.
So far, I can check if use liked the page using the following FQL:
public void checkIfLikes(View v) {
final Session session = Session.getActiveSession();
AsyncTask.execute(new Runnable() {
public void run() {
try {
facebookClient = new DefaultFacebookClient(session.getAccessToken());
User user = facebookClient.fetchObject("me", User.class);
Page page = facebookClient.fetchObject("GeoWhere", Page.class);
String query = "SELECT uid FROM page_fan WHERE page_id=" + page.getId() + " and uid=" + user.getId();
List<FqlUser> users = facebookClient.executeFqlQuery(query, FqlUser.class);
} catch(com.restfb.exception.FacebookException ex) {
Log.e(TAG, "1" + ex.getMessage());
} catch(Exception e) {
Log.e(TAG, "2" + e.getMessage() + e.getStackTrace().toString());
}
}
});
}
Can anyone help me achieve this?
And most importantly, I can't find anywhere on the Facebook v3 API for Android a nice way to open the FanPage. Do I really have to use the WebView? I don't like the way the user has to leave my app to Like the fan page... I don't like the UX of this...
Thanks a bunch, Felipe
EDIT 1: I am invoking this code now to go to my page:
public static Intent getOpenFacebookIntent(Context context) {
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/104585606418628"));
} catch (Exception e) {
Log.e(TAG, ""+e.getMessage());
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/GeoWhere"));
}
}
It works well and I can like the page. When pressing back button and returning to my Activity, onResume is not invoked. In fact, the whole activity is hanging. Buttons are not answering. When I press back again, onResume is invoked, but then I fail with: User canceled operation.
and this is happening at com.facebook.widget.LoginButton
. Very odd.
EDIT 2
Seems like I fixed it. Was invoking the startActivity() inside the call() method of Facebook and that was making the call() method not to completely finish. So the state of the Session was not open. So what I did was:
public void call(Session session, SessionState state, Exception exception) {
facebookClient = new DefaultFacebookClient(session.getAccessToken());
//startActivity(getOpenFacebookIntent(MainActivity.this));
//launchFacebook();
//Intent i = new Intent(MainActivity.this, FacebookWebViewActivity.class);
//startActivity(i);
IS_SOCIAL = true;
}
And somewhere else in the code I check if the user is logged on Facebook and proceed with sending him to the fan page
Take care !!
This is how you can check if someone like a page, you need both user id and the page id.
/* make the API call */
new Request(
session,
"/{user-id}/likes/{page-id}",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With