10

I used to open my facebook page from my app using the below code, but this does not work anymore starting facebook v11.0.0.11.23 released on June 21, 2014, any idea how to open the page in the new facebook app? To note that it opens now the facebook app but without opening the specified page, it used to work just fine before the latest update

public void openFacebookPage() {
    Intent intent = null;
    try {
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id here>"));
        //tried this also
        //intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<id here>"));
    } catch (Exception e) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<name here>"));
    }
    context.startActivity(intent);
}
sam
  • 4,357
  • 5
  • 29
  • 34

1 Answers1

31

In Facebook version 11.0.0.11.23 (3002850) fb://profile/ and fb://page/ are no longer supported. I decompiled the Facebook app and was able to come up with the following solution:

String facebookUrl = "https://www.facebook.com/JRummyApps";
try {
    int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
    if (versionCode >= 3002850) {
        Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));;
    } else {
        // open the Facebook app using the old method (fb://profile/id or fb://page/id)
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/336227679757310")));
    }
} catch (PackageManager.NameNotFoundException e) {
    // Facebook is not installed. Open the browser
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
}

Edit: It has been some time and it looks like fb://profile and fb://page are no longer supported. Below is the method I have been using in production:

/**
 * Intent to open the official Facebook app. If the Facebook app is not installed then the
 * default web browser will be used.</p>
 * 
 * Example usage:</p>
 * <code>newFacebookIntent(context.getPackageManager(), "https://www.facebook.com/JRummyApps");</code></p>
 * 
 * @param pm
 *            Instance of the {@link PackageManager}.
 * @param url
 *            The full URL to the Facebook page or profile.
 * @return An intent that will open the Facebook page/profile.
 */
public static Intent newFacebookIntent(PackageManager pm, String url) {
    Uri uri;
    try {
        pm.getPackageInfo("com.facebook.katana", 0);
        // http://stackoverflow.com/a/24547437/1048340
        uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    } catch (PackageManager.NameNotFoundException e) {
        uri = Uri.parse(url);
    }
    return new Intent(Intent.ACTION_VIEW, uri);
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Thanks I tried it, it works fine by loading the web page within the native facebook app, although I would have preferred a more integrated solution (same page layout as if opening the page from within the facebook app), I will wait a little bit, if no better solution is provided, I will accept this one. Thank you – sam Jul 03 '14 at 20:44
  • Cool. I hope someone comes up with something better. That took some digging to find. – Jared Rummler Jul 03 '14 at 21:08
  • I used your code and despite the fact that it open Facebook application I get the message "Page not found". What's wrong with my use? I know url is right because I use it in another intent and it works! – arniotaki Jul 07 '14 at 06:45
  • 1
    I found it. You first have to change the username of your page if you haven't done it yet from here: www.facebook.com/username – arniotaki Jul 07 '14 at 07:15
  • 2
    This answer needs to start coming up first in Google searches. All the answers that show up now are old and don't work anymore. Thank you for looking into this... it works as of this date! – hellaandrew Jan 23 '16 at 20:53
  • it works but then when I press back button it opens facebook home page and I need to press back one more time to get back to my app – Ana Koridze Jun 08 '16 at 12:27