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:
- Go to your page
- Right-click and
View Page Source
- Find in page:
fb://page/?id=
- Here you go!