1

It seemed a foregone question and I thought I could easily find an answer already, but the answers I found are very old and they do not work. How do I create a intent from my app to facebook page? I would like that if there is open the Facebook app, otherwise the browser

Up to now the best SOLUTION is that of Rishav Singla:

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>"));
    }
}

use:

startActivity(getOpenFacebookIntent(getApplicationContext()));

... in attesa di altre soluzioni

Antonino Chiaia
  • 37
  • 2
  • 10
  • check if `com.facebook.katana` package is available using `packageManager` if its available then pass the data to the app else open the browser with fb url.. – Santanu Sur Sep 24 '18 at 12:58

3 Answers3

2

You can use below code to redirect to specific user Your facebook app would be in background, so in order to open it you have to run this code twice(That's a weird behaviour :( )

try
    {
        Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<your profile_id>"));
        startActivity(followIntent);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run() {
                Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<your profile_id>"));
                startActivity(followIntent);
            }
        }, 1000 * 2);

    }
    catch (Exception e)
    {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name>")));
        String errorMessage = (e.getMessage()==null)?"Message is empty":e.getMessage();
        Log.e("Unlock_ScreenActivity:FacebookAppNotFound" ,errorMessage);
    }

Hope this helps you Happy coding!

Vikash Bijarniya
  • 404
  • 4
  • 10
  • it does not seem like a clean procedure, in fact if I do back on one of the two activities that open I get an error from the system – Antonino Chiaia Sep 24 '18 at 13:18
1

Use URL to facebook to Open it in browser

    String url = "https://www.facebook.com/ZambianWatchdog/";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
0
public Intent getIntent() {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/{{page}}"));
    if (intent.resolveActivity(getPackageManager()) != null) {
        return intent;
    }
    return null;
}
Naim.Jusufi
  • 125
  • 9