Open telegram channel in android

Solution 1:

Intent for opening a Telegram channel or user :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=partsilicon"));
startActivity(intent);

Solution 2:

First check if any telegram client (telegram messenger or telegram x) is installed. If not open it in browser.

fun telegramIntent(context: Context): Intent {
    var intent: Intent? = null
    try {
        try {
           context.packageManager.getPackageInfo("org.telegram.messenger", 0)//Check for Telegram Messenger App
        } catch (e : Exception){
           context.packageManager.getPackageInfo("org.thunderdog.challegram", 0)//Check for Telegram X App
        }
       intent = Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=${TELEGRAM_PAGE_ID}"))
    }catch (e : Exception){ //App not found open in browser
       intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.telegram.me/$TELEGRAM_PAGE_ID"))
    }
   return intent!!
}

Solution 3:

Answer @Saurabh Padwekar is good, but i want tell that you probably need add queries for Android SDK 30+ like this:

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="tg" />
    </intent>
</queries>

Solution 4:

Java version of @Saurabh Padwekar 's answer!

  Intent getTelegramInt(Context context) {
        Intent intent;
        try {
            try { // check for telegram app
                context.getPackageManager().getPackageInfo("org.telegram.messenger", 0);
            } catch (PackageManager.NameNotFoundException e) {
                // check for telegram X app
                context.getPackageManager().getPackageInfo("org.thunderdog.challegram", 0);
            }
            // set app Uri
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tg://resolve?domain=${TELEGRAM_PAGE_ID}"));
        } catch (PackageManager.NameNotFoundException e) {
            // set browser URI
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.telegram.me/$TELEGRAM_PAGE_ID"));
        }
        return intent;
    }