0

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:

  1. Image button is clicked
  2. Call to Facebook Graph API is made to get the link to the user's profile via their Facebook ID (stored in our database)
  3. A new Intent is created by checking to see if the Facebook app is installed, and generating the correct Uri
  4. A new Activity is started with the generated Intent

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:

  1. 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}
  2. 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:

  1. An app-scoped link
  2. 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 WebViews.

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?

Community
  • 1
  • 1
Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75

1 Answers1

1

You should be able to use the fb://profile/{user-id} URL format to launch native apps. This will let you use the user ID instead of a username. You could rewrite your getFacebookIntent method as:

public Intent getFacebookIntent(GraphUser user) {
    PackageManager pm = this.getPackageManager();
    Uri uri;
    try {
        pm.getPackageInfo("com.facebook.katana", 0);
        uri = Uri.parse("fb://profile/" + user.getId());
    } catch (PackageManager.NameNotFoundException e) {
        uri = Uri.parse(user.getLink());
    }
    return new Intent(Intent.ACTION_VIEW, uri);
}

You could then call this method and pass in the GraphUser object.

subeeshb
  • 478
  • 4
  • 10
  • Oddly enough I ended up trying this before you had posted your answer. I forgot about this question after that, but I'm back now to make sure I gave you a +1 and accepted. Thanks! – Chris Cirefice May 13 '15 at 16:01