How to Listen Incoming SMS in Flutter

In this example, we are going to show you how to listen to incoming SMS text in Flutter. You may need to listen to incoming SMS for functionality like OTP autofill form fields, SMS app, or anywhere you need to read incoming SMS. See the example below:

First, Add telephony plugin to your project by adding the following lines in pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  telephony: ^0.2.0

Import the plugin to your script:

import 'package:telephony/telephony.dart';

Now add this permission and tags on AndroidManifest.xml file at android/app/src/main folder:

<manifest>
	<uses-permission android:name="android.permission.RECEIVE_SMS"/>

	<application>
		...
		...

		<receiver android:name="com.shounakmulay.telephony.sms.IncomingSmsReceiver"
		    android:permission="android.permission.BROADCAST_SMS" android:exported="true">
		    <intent-filter>
			<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
		    </intent-filter>
		</receiver>

	</application>
</manifest>

Your AndroidManifest.xml file will look like the below:

No on script section, you can listen to the incoming messages with the following code:

Telephony telephony = Telephony.instance;
telephony.listenIncomingSms(
    onNewMessage: (SmsMessage message) {
        print(message.address); //+977981******67, sender nubmer
        print(message.body); //sms text
        print(message.date); //1659690242000, timestamp
    },
    listenInBackground: false,
);

Here, you can get the sender number, the SMS text, and the timestamp when SMS is received. You can convert the time stamp to date as well.

import 'package:flutter/material.dart';
import 'package:telephony/telephony.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {  

  String sms = ""; 
  Telephony telephony = Telephony.instance;

  @override
  void initState() {
      telephony.listenIncomingSms(
          onNewMessage: (SmsMessage message) {
              print(message.address); //+977981******67, sender nubmer
              print(message.body); //sms text
              print(message.date); //1659690242000, timestamp
              setState(() {
                  sms = message.body.toString();
              });
          },
          listenInBackground: false,
      );
      super.initState();
  }

  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          appBar: AppBar(
            title: Text("Listen Incoming SMS in Flutter"),
            backgroundColor: Colors.redAccent
          ),
          body: Container(
            padding: EdgeInsets.only(top:50, left:20, right:20),
            alignment: Alignment.topLeft,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [ 
                
                Text("Recieved SMS Text:", style: TextStyle(fontSize: 30),),
                Divider(),
                Text("SMS Text:" + sms, style: TextStyle(fontSize: 20),)

            ],)
          )
       );
  }
}

In this way, you can listen to incoming SMS in the Flutter app.

No any Comments on this Article


Please Wait...