Developer

How to use this hack to open a specific Facebook page from your Android app

A try-catch-block is the key to deep linking your Android app to Facebook regardless of whether the user has the native Facebook app on her device.

facebook-cred-cnetuk-092414.jpg
Image: CNET UK

Of all the third-party apps I interact with as part of my development tasks, Facebook can be one of the most frustrating. Not only does it change frequently, but it also doesn't always behave as advertised.

Recently I had some issues trying to open a specific page in Facebook. The use case was straightforward enough. In the game I was working on, there was a Facebook icon, and clicking that icon should take the user to the game's fan page. My first thought was I'd simply launch an intent using the HTTP URL of the page.

facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/MelodiousGame"));
startActivity(facebookAppIntent);

This worked fine on the simulator, but when I put it on my actual device, instead of opening the page in a browser window, the native Facebook app launched, landing on my own home page rather than the fan page I intended.

As it turns out, there is a custom schema for the Facebook app that looks like this:

facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/335853576565939"));
startActivity(facebookAppIntent);

But wait, what's that string of digits? The custom Uri for the native Facebook app needs a page ID, not the name, so how does one go about getting the ID of a Facebook page?

Luckily, it turns out it can be retrieved using an unauthenticated Graph API call. So if you, for example, throw https://graph.facebook.com/MelodiousGame into a web browser, you'll get back a big old JSON object, and in the outermost node you will find the ID you want. Armed with the ID, and the knowledge that I'd have to launch a different intent depending on whether the device had the native Facebook app installed, the final solution was just a matter of a well-placed try-catch-block.

Intent facebookAppIntent;
        try {
            facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/335853576565939"));
            startActivity(facebookAppIntent);
        } catch (ActivityNotFoundException e) {
            facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/MelodiousGame"));
            startActivity(facebookAppIntent);
        }
 

Ta-da! Now you can deep link your app to Facebook regardless of whether the user has the native app on her device.

Figure A

melodiusfacebookfiga.png
Image: William J. Francis

User experience when the user does not have the native Facebook app installed.

Figure B

melodiusfacebookfigb.png
Image: William J. Francis

User experience link when the user does have the native Facebook app installed.

Note: Melodious is my latest Android app game.

About William J. Francis

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

Editor's Picks

Free Newsletters, In your Inbox