How to send a Intent with telegram
Asked Answered
O

5

24

I am trying to create a class in java which manages different social sharing apps. The class is based on android intents.

but when I try to execute Telegram intent, it doesn't find the app.

Here I put the code I have written:

public void shareTelegram(String message)
{
            Intent waIntent = new Intent(Intent.ACTION_SEND);
            waIntent.setType("text/plain");
            waIntent.setPackage("com.telegram");
            if (waIntent != null) {
                waIntent.putExtra(Intent.EXTRA_TEXT, message);//
                _androidActivity.startActivity(Intent.createChooser(waIntent, "Share with"));
            } 
            else 
            {
                Toast.makeText(_androidActivity.getApplicationContext(), "Telegram is not installed", Toast.LENGTH_SHORT).show();
            }

}

Where could I find the package name? Thanks in advance.

Oba answered 7/2, 2014 at 11:55 Comment(2)
do you want to know the package name of Telegram app?Cherenkov
play.google.com/store/apps/… Please check your package name of telegram app org.telegram.messengerItalianate
D
43

All Android app have an unique ID, market ID. If you look into Google Play or google search market://details?id=org.telegram, It send you to

https://play.google.com/store/apps/details?id=org.telegram.messenger

If you send the intent with:

waIntent.setPackage("org.telegram.messenger");

It will work.

If you prefer a little bit complex system I recommend you to use:

/**
     * Intent to send a telegram message
     * @param msg
     */
    void intentMessageTelegram(String msg)
    {
        final String appName = "org.telegram.messenger";
        final boolean isAppInstalled = isAppAvailable(mUIActivity.getApplicationContext(), appName);
        if (isAppInstalled) 
        {
            Intent myIntent = new Intent(Intent.ACTION_SEND);
            myIntent.setType("text/plain");
            myIntent.setPackage(appName);
            myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
            mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
        } 
        else 
        {
            Toast.makeText(mUIActivity, "Telegram not Installed", Toast.LENGTH_SHORT).show();
        }
    }

And check if is installed with:

/**
         * Indicates whether the specified app ins installed and can used as an intent. This
         * method checks the package manager for installed packages that can
         * respond to an intent with the specified app. If no suitable package is
         * found, this method returns false.
         *
         * @param context The application's environment.
         * @param appName The name of the package you want to check
         *
         * @return True if app is installed
         */
        public static boolean isAppAvailable(Context context, String appName) 
        {
            PackageManager pm = context.getPackageManager();
            try 
            {
                pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
                return true;
            } 
            catch (NameNotFoundException e) 
            {
                return false;
            }
        }
Decelerate answered 7/2, 2014 at 12:0 Comment(2)
I think telegram is a bit changed since I get the "No Apps can perform this action" whenever I start the intent.Hallucinogen
Starting API 30 dont forget to add permission developer.android.com/reference/kotlin/android/… to get other apps infoDeil
S
13

For opening telegram channel :

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://telegram.me/shes_ir"));
final String appName = "org.telegram.messenger";
try {
    if (isAppAvailable(mainActivity.getApplicationContext(), appName))
        i.setPackage(appName);
} catch (PackageManager.NameNotFoundException e) {}
mainActivity.startActivity(i);
Selfrevealing answered 10/4, 2016 at 15:16 Comment(0)
L
4
> **//open telegram directly without intent to specify id.**


 Intent telegram = new Intent(android.content.Intent.ACTION_SEND);
     telegram.setData(Uri.parse("http://telegram.me/myId"));
     telegram.setPackage("org.telegram.messenger");
     Test.this.startActivity(Intent.createChooser(telegram, "Share with"));
Luckey answered 17/1, 2016 at 23:55 Comment(3)
What's "myId" ? How do you get it, when you have a phone number?Zyrian
u need to use user name instead. check my answerSecretariat
how can this be done without username? because someusers dont give username to thier id, its not mandatoryWilliam
B
1
  void intentMessageTelegram(String msg)
    {
        final String appName = "org.telegram.messenger";
        final boolean isAppInstalled = isAppAvailable(this.getApplicationContext(), appName);
        if (isAppInstalled)
        {
            Intent myIntent = new Intent(Intent.ACTION_SEND);
            myIntent.setType("text/plain");
            myIntent.setPackage(appName);
            myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
            this.startActivity(Intent.createChooser(myIntent, "Share with"));
        }
        else
        {
            Toast.makeText(this, "Telegram not Installed", Toast.LENGTH_SHORT).show();
        }
    }
    public static boolean isAppAvailable(Context context, String appName)
    {
        PackageManager pm = context.getPackageManager();
        try
        {
            pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

  btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                intentMessageTelegram("Hi");
            }
        });
Busty answered 5/10, 2017 at 21:29 Comment(1)
I made little change in vgonisanz's post and use it in MainAcitvity and It works at least on Android 6 !Busty
Z
1

In case you wish to just open the chat with some phone number, it's as such:

/**@param fullPhoneNumber universal phone number, meaning including "+" and the country phone prefix */
fun getTelegramChatIntentFromPhoneNumber(fullPhoneNumber: String): Intent {
    val uri =
        Uri.Builder().scheme("http").authority("telegram.me").appendEncodedPath(fullPhoneNumber).build()
    return Intent(Intent.ACTION_VIEW, uri).setPackage("org.telegram.messenger")
}

Note that you can remove the "setPackage" part in case you wish to support all apps that can handle it.

A similar thing is available for WhatsApp, BTW:

fun prepareWhatsAppMessageIntent(fullPhoneNumber: String, message: String? = null): Intent {
    val builder = Uri.Builder().scheme("https").authority("api.whatsapp.com").path("send")
    builder.appendQueryParameter("phone", fullPhoneNumber)
    message?.let { builder.appendQueryParameter("text", it) }
    return Intent(Intent.ACTION_VIEW, builder.build())
}
Zyrian answered 11/8, 2022 at 7:55 Comment(2)
Thanks. How can I add a pre-text for Telegram. I tried to use .appendQueryParameter("text", "hello") but it not workingChalcography
@Chalcography Perhaps it doesn't support it? Maybe try to generate this: github.com/TelegramMessenger/Telegram-iOS/issues/15 , meaning tg://msg?text=Hello&to=+42333Zyrian

© 2022 - 2024 — McMap. All rights reserved.