32

I would like to open my Facebook page from my Android app, if the Facebook app is available - if not: this page should be open in the default browser.

for this i tried the following code:

try {
  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/MY_PAGE_ID"));
startActivity(intent);
} catch (Exception e) {
   startActivity(new Intent(Intent.ACTION_VIEW,    Uri.parse("http://www.facebook.com/MY_PAGE_NAME")));
}

Problem is: I have here an Android device with the newest version of Facebook. If I would like to open from my app the Facebook page, the Facebook app will open , but without my page.

I only see the message:

Trouble Loading Timeline.

What is wrong?

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
SpecialFighter
  • 573
  • 1
  • 9
  • 14

6 Answers6

127

"fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions.

This is a full fledged working code currently live in one of my apps:

public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";

//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
            if (versionCode >= 3002850) { //newer versions of fb app
                return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
            } else { //older versions of fb app
                return "fb://page/" + FACEBOOK_PAGE_ID;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL; //normal web url
        }
    }

This method will return the correct url for app if installed or web url if app is not installed.

Then start an intent as follows:

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);

That's all you need.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • source: http://stackoverflow.com/a/24547437/1048340 – Jared Rummler Jan 02 '16 at 09:19
  • That worked like charm – Adnan Jun 22 '16 at 08:09
  • 2
    Works *almost* all the time. This crashes if the user has the Facebook app installed, but disabled. – scottrick Sep 13 '16 at 15:45
  • 1
    Gud catch... Catch the specific exception... Also feel free to add it to the answer... – Viral Patel Sep 13 '16 at 16:54
  • what if i have only the facebok id rather than the [pagename] – Raafat Alhmidi Oct 27 '16 at 07:44
  • basically you just need to open the page and use the string that follows `https://www.facebook.com/`. This is generally the page name. If page name is not set explicitly then use the string that appears after the facebook domain `https://www.facebook.com/` – Viral Patel Oct 27 '16 at 08:55
  • 3
    i used this and it works but there is no like button for my page, also contents are a bit different.. – Arthas Feb 07 '17 at 09:59
  • @Arthas thats strange. Can you share a screenshot showing the differences? – Viral Patel Feb 07 '17 at 10:00
  • 1
    @AndroidMechanic when i open my page with your method it shows about , videos , photos and posts , However the other options like like etc are missing. I'll add screenshots – Arthas Feb 07 '17 at 10:12
  • @AndroidMechanic http://imgur.com/a/U1Qeu You can check here : D – Arthas Feb 07 '17 at 10:19
  • Happening in my app too. was working fine earlier. Looks like a change in fb app. I'll try checking if there is a workaround and will update the answer if i find something. – Viral Patel Feb 08 '17 at 10:22
  • @AndroidMechanic cool , dont you think we should have an offical documented mechanism to open link in social apps? – Arthas Feb 09 '17 at 09:12
  • http://www.techrepublic.com/article/pro-tip-use-this-hack-to-open-a-specific-facebook-page-from-your-android-app/ found this and its working check it out ! – Arthas Feb 09 '17 at 09:58
  • 7
    until now Feb 20th, 2017: *"fb://profile/MY_PAGE_ID"* : not working ; *"fb://facewebmodal/f?href=" + FACEBOOK_URL* : go to the page but not home page (without LIKE or Contact Us ... button). ; *"fb://page/PAGE_ID"* : WORKING FINE ! – Phong Nguyen Feb 20 '17 at 08:19
  • 4
    `fb://page/` still works on the newer version. My facebook app is up to date, and it still opens my page correctly. – Kimi Chiu Mar 07 '17 at 07:38
  • 1
    Today facebook does not allow unauthorized graph queries for pages. I managed to get the id of a facebook page by opening the page in an incognito tab, the first xhr request holds the page id in the query parameter. – Angel Venchev May 24 '17 at 15:30
  • 1
    ApplicationInfo.versionCode seems to be no longer accessible – SjoerdvGestel Jan 04 '18 at 13:45
  • Please help to open Messenger by facebook user id? – Konstantin Konopko Jan 29 '18 at 17:01
  • ApplicationInfo.versionCode is now deprecated in API Level 29. Also this crashes if the user installed the facebook app but didn't login. – Szörényi Ádám Oct 14 '19 at 20:40
  • I'm trying to open a specific post on a Facebook page, which works fine on iOS with `fb://profile/123/posts?id=456`. On Android, the `fb://page/123`still works for me, but there is no way to pass a parameter for the expected post. It's possible to do this by using fb://facewebmodal/f?href=" and passing the URL as "https://www.facebook.com/123/posts/456". Is it possible to do this through `fb://page`? – Gold.strike Sep 21 '20 at 10:08
17

This works on the latest version:

Go to https://graph.facebook.com/ (https://graph.facebook.com/fsintents for instance).

Copy your id Use this method:

public static Intent getOpenFacebookIntent(Context context) {
    try {
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
        return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
    } catch (Exception e) {
        return new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.facebook.com/<user_name_here>"));
    }
}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
Yash Jain
  • 189
  • 7
  • 4
    This is the best answer for my need. The "fb://facewebmodal/f?href=" answer above for newer versions takes users to a page of posts by the entity, not really their "page". Did not research this further. Also, very important that you use the ID of the page NOT the vanity page name!! – EricWasTaken Aug 15 '16 at 21:42
  • 2
    Agree. This opens the actual page, not a search after the page. For those having trouble getting the page id, get it from here: http://findmyfbid.com/ –  Dec 13 '16 at 20:49
  • not opening the facebook app. Always open the browser even facebook app is installed – Bilal Bangash Sep 17 '21 at 12:40
12

Okay I modifed @AndroidMechanics Code, because on devices were facebook is disabled the app crashes!

here is the modifed getFacebookUrl:

public String getFacebookPageURL(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;

            boolean activated =  packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
            if(activated){
                if ((versionCode >= 3002850)) { 
                    return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
                } else { 
                    return "fb://page/" + FACEBOOK_PAGE_ID;
                }
            }else{
                return FACEBOOK_URL;
            }
         } catch (PackageManager.NameNotFoundException e) {
            return FACEBOOK_URL; 
        }
    }

The only added thing is to look if the app is disabled or not if it is disabled the app will call the webbrowser!

Seref
  • 123
  • 1
  • 2
  • 5
7

Here's a solution that mixes the code by Jared Rummler and AndroidMechanic.

Note: fb://facewebmodal/f?href= redirects to a weird facebook page that doesn't have the like and other important buttons, which is why I try fb://page/. It works fine with the current Facebook version (126.0.0.21.77, June 1st 2017). The catch might be useless, I left it just in case.

public static String getFacebookPageURL(Context context)
{
    final String FACEBOOK_PAGE_ID = "123456789";
    final String FACEBOOK_URL = "MyFacebookPage";

    if(appInstalledOrNot(context, "com.facebook.katana"))
    {
        try
        {
            return "fb://page/" + FACEBOOK_PAGE_ID;
            // previous version, maybe relevant for old android APIs ?
            // return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
        }
        catch(Exception e) {}
    }
    else
    {
        return FACEBOOK_URL;
    }

}

Here's the appInstalledOrNot function which I took (and modified) from Aerrow's answer to this post

private static boolean appInstalledOrNot(Context context, String uri)
{
    PackageManager pm = context.getPackageManager();
    try
    {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        return true;
    }
    catch(PackageManager.NameNotFoundException e)
    {
    }

    return false;
}

How to get the Facebook ID of a page:

  1. Go to your page
  2. Right-click and View Page Source
  3. Find in page: fb://page/?id=
  4. Here you go!
Julian Honma
  • 1,674
  • 14
  • 22
2

you can use this:

try {
                Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" +
                        "https://www.facebook.com/app_scoped_user_id/"+scoped user id+"/"));
                activity.startActivity(followIntent);
            } catch (Exception e) {
                activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + user name)));
                String errorMessage = (e.getMessage() == null) ? "Message is empty" : e.getMessage();
            }

attention: you can get scoped user id from "link" permission facebook api

Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
-3

I already have answered here and it's working for me, please refer this link https://stackoverflow.com/a/40133225/3636561

    String socailLink="https://www.facebook.com/kfc";
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink);
    if (facebookUrl == null || facebookUrl.length() == 0) {
        Log.d("facebook Url", " is coming as " + facebookUrl);
        return;
    }
    intent.setData(Uri.parse(facebookUrl));
    startActivity(intent);

please refer link to get rest part.

Community
  • 1
  • 1
Abdul Rizwan
  • 3,904
  • 32
  • 31