Note: This issue only arises when the Facebook app is installed.
I'm trying to open a user's Facebook profile page when an ImageButton
is clicked. After looking at this suggested method for more recent Facebook app versions, it's mostly working, with the exception of users that are friends with the logged in user (in the app), or the logged in user themselves.
The process is:
- Image button is clicked
- Call to Facebook Graph API is made to get the link to the user's profile via their Facebook ID (stored in our database)
- A new
Intent
is created by checking to see if the Facebook app is installed, and generating the correct Uri - A new
Activity
is started with the generatedIntent
Because we're using Facebook Graph API v2.0, the username
field is removed from the GraphUser
object in the JSON. The problem is that the Facebook link (GraphUser.getLink()
) is either:
- An app-scoped (protected) link if the user (logged into the app) is not friends with the user whose profile we are trying to view; format is :
https://www.facebook.com/app_scoped_user_id/{protected-id}
- A 'normal' Facebook link with the user's real id (unprotected); format is:
http://www.facebook.com/{real-user-id}
The real problem is that the Facebook Uri when the app is installed expects either:
- An app-scoped link
- A link with the
username
attached to it, not{user-id}
However, the web version (opened in a web view) doesn't care. The link can be app-scoped, username-based or id-based. So everything works for WebView
s.
Since username
is completely inaccessible in Graph API v2.0, I'm just getting null
values, which means that for the Facebook app I can't generate the correct Uri that it expects (username-based), only id-based or app-scoped.
Here is the code that I'm using to create the Intent
:
public static Intent getFacebookIntent(String facebookProfileUrl) {
PackageManager pm = App.getContext().getPackageManager();
Uri uri;
try {
pm.getPackageInfo("com.facebook.katana", 0);
uri = Uri.parse("fb://facewebmodal/f?href=" + facebookProfileUrl);
} catch (PackageManager.NameNotFoundException e) {
uri = Uri.parse(facebookProfileUrl);
}
return new Intent(Intent.ACTION_VIEW, uri);
}
And here is my onClick()
method on the ImageButton
:
new Request(
Session.getActiveSession(),
String.valueOf(mate.getFacebookId()),
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
GraphUser g = response.getGraphObjectAs(GraphUser.class);
String fbUrl = g.getLink();
// TODO: If link does not contain 'scoped', need USERNAME, not ID
String otherUrl = "https://www.facebook.com/" + myAppUser.getId();
String finalUrl = (fbUrl.contains("scoped")) ? fbUrl : otherUrl;
Intent intent = getFacebookIntent(finalUrl);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
).executeAsync();
I need the username
if the app is installed, otherwise the web view deals with it as it should.
So the question: How can I get a Facebook username by id for the Facebook app? Or, is there a different Uri that the Facebook app expects that I can account for, using ids and not usernames?