Flutter how to open WhatsApp, Viber, Telegram or SMS Messenger and send Text Message

Anton Ustinoff
4 min readAug 24, 2023

In Flutter open WhatsApp, Viber, Telegram or SMS Messenger and send text message is very easy. You can do it without using Flutter Share Plugin. The only one of plugin you should use it is url_launcher. If You want to launch Messenger application from Flutter code with predefine text message then you should follow this tutorial Flutter how to open WhatsApp, Viber, Telegram or SMS Messenger and send Text Message.

This Flutter code is working on both iPhone & Android. So I will suggest you to run this code on real mobile phone not on emulator. Messenger must installed on your mobile phone. Otherwise you will get an error “Messenger is not installed”.

First step

You need to add this key and values to info.plist for correctly work on iOS devices.

<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
<string>viber</string>
<string>tg</string>
<string>itms-beta</string>
<string>itms</string>
</array>

And in AndroidManifest.xml for correctly work on Android devices.

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="sms" />
</intent> <intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tel" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tg" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>

Send message with SMS Messenger

Use bellow utility method to open SMS Messenger on different devices with text message.

import 'package:url_launcher/url_launcher.dart';

/// A utility method to open SMS Messenger on different devices
/// Optionality you can add [text] message
Future<void> openSMS({
required String phone,
String? text,
LaunchMode mode = LaunchMode.externalApplication,
}) async {
final String effectivePhone = Platform.isAndroid
? phone.replaceAll('-', ' ')
: phone.replaceFirst('+', '');

final String effectiveText =
Platform.isAndroid ? '?body=$text' : '&body=$text';

final String url = 'sms:$effectivePhone';

if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(Uri.parse('$url$effectiveText'), mode: mode);
} else {
throw Exception('openSMS could not launching url: $url');
}
}

Use in your code.

GestureDetector(
child: Text('Send message'),
onTap: () async {
try {
await openSMS(
phone: '+0 (000) 000-0000',
text: 'Initial text',
);
} on Exception catch (e) {
showDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: "Attention",
content: Padding(
padding: const EdgeInsets.only(top: 5),
child: Text(
'We did not find the «SMS Messenger» application on your phone, please install it and try again»',
style: context.theme.textTheme.labelSmall?.copyWith(
height: 1.1,
color: context.theme.textTheme.bodyLarge?.color,
),
),
),
actions: [
CupertinoDialogAction(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
),
],
)
);
}
},
)

Send message with WhatsApp

Use bellow utility method to open WhatsApp on different devices with text message.

import 'package:url_launcher/url_launcher.dart';

/// A utility method to open WhatsApp on different devices
/// Optionality you can add [text] message
Future<void> openWhatsApp({
required String phone,
String? text,
LaunchMode mode = LaunchMode.externalApplication,
}) async {
final String textIOS = text != null ? Uri.encodeFull('?text=$text') : '';
final String textAndroid = text != null ? Uri.encodeFull('&text=$text') : '';

final String urlIOS = 'https://wa.me/$phone$textIOS';
final String urlAndroid = 'whatsapp://send/?phone=$phone$textAndroid';

final String effectiveURL = Platform.isIOS ? urlIOS : urlAndroid;

if (await canLaunchUrl(Uri.parse(effectiveURL))) {
await launchUrl(Uri.parse(effectiveURL), mode: mode);
} else {
throw Exception('openWhatsApp could not launching url: $effectiveURL');
}
}

Use in your code.

GestureDetector(
child: Text('Send message'),
onTap: () async {
try {
await openWhatsApp(
phone: '+0 (000) 000-0000',
text: 'Initial text',
);
} on Exception catch (e) {
showDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: "Attention",
content: Padding(
padding: const EdgeInsets.only(top: 5),
child: Text(
'We did not find the «WhatsApp» application on your phone, please install it and try again',
style: context.theme.textTheme.labelSmall?.copyWith(
height: 1.1,
color: context.theme.textTheme.bodyLarge?.color,
),
),
),
actions: [
CupertinoDialogAction(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
),
],
)
);
}
},
)

Send message with Telegram

Use bellow utility method to open Telegram on different devices with text message.

import 'package:url_launcher/url_launcher.dart';

/// A utility method to open WhatsApp on different devices
/// Optionality you can add [text] message
Future<void> openTelegram({
required String phone,
String? text,
LaunchMode mode = LaunchMode.externalApplication,
}) async {
const String url = 'tg://msg';
final String? effectiveText = text != null && text!.isNotEmpty
? '&text=${Uri.encodeFull(text!)}'
: null;

if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(
Uri.parse('$url?to=+$phone$effectiveText'),
mode: mode,
);
} else {
throw Exception('openTelegram could not launching url: ${$url?to=+$phone$effectiveText}');
}
}

Use in your code.

GestureDetector(
child: Text('Send message'),
onTap: () async {
try {
await openTelegram(
phone: '+0 (000) 000-0000',
text: 'Initial text',
);
} on Exception catch (e) {
showDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: "Attention",
content: Padding(
padding: const EdgeInsets.only(top: 5),
child: Text(
'We did not find the «Telegram» application on your phone, please install it and try again',
style: context.theme.textTheme.labelSmall?.copyWith(
height: 1.1,
color: context.theme.textTheme.bodyLarge?.color,
),
),
),
actions: [
CupertinoDialogAction(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
),
],
)
);
}
},
)

Send message with Viber

Use bellow utility method to open Viber on different devices with text message.

import 'package:url_launcher/url_launcher.dart';

/// A utility method to open WhatsApp on different devices
/// Optionality you can add [text] message
Future<void> openViber({
required String phone,
String? text,
LaunchMode mode = LaunchMode.externalApplication,
}) async {
final String url = 'viber://chat?number=$phone';
final String effectiveURL = text != null && text!.isNotEmpty
? '$url&draft=${Uri.encodeFull(text!)}'
: url;

if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(
Uri.parse(Uri.parse(effectiveURL)),
mode: mode,
);
} else {
throw Exception('openViber could not launching url: $effectiveURL');
}
}

Use in your code.

GestureDetector(
child: Text('Send message'),
onTap: () async {
try {
await openViber(
phone: '+0 (000) 000-0000',
text: 'Initial text',
);
} on Exception catch (e) {
showDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: "Attention",
content: Padding(
padding: const EdgeInsets.only(top: 5),
child: Text(
'We did not find the «Viber» application on your phone, please install it and try again',
style: context.theme.textTheme.labelSmall?.copyWith(
height: 1.1,
color: context.theme.textTheme.bodyLarge?.color,
),
),
),
actions: [
CupertinoDialogAction(
child: Text('Close'),
onPressed: () => Navigator.of(context).pop(),
),
],
)
);
}
},
)

Now run the code on real mobile phone. Flutter how to open WhatsApp, Viber, Telegram or SMS Messenger and send Text Message.

Thanks for reading. If you have any questions please feel free to contact me.

--

--

Anton Ustinoff

Hey! I’m a software engineer and a mobile developer. I have been developing software for 8 years and developing mobile apps with Flutter and SwiftUI for 3 years