How to read inbox SMS messages using flutter

Learn how to read inbox SMS messages in your Flutter app with this comprehensive guide. This guide provides step-by-step instructions and practical examples to help you seamlessly incorporate this feature into your app.

Photo by Christopher Gower on Unsplash

Applications for SMS reading in your app are vast. For instance, you can implement OTP verification to streamline user authentication, reducing friction and increasing security. Another useful application is SMS-based login, which simplifies the login process for users by allowing them to log in using a one-time code sent via SMS. Additionally, you can use SMS reading for transaction alerts, ensuring users receive real-time updates on their financial activities directly within the app. Boost your app’s functionality and user satisfaction by mastering SMS reading with Flutter.

Disclaimer: This implementation will only work for android devices.

1. Install Flutter Packages

We are going to make use of the Flutter SMS Inbox library to help read inbox sms messages. Please support the open source project here. To get started we’ll need two packages to be install into our project; flutter_sms_inbox and permission_handler

flutter pub add flutter_sms_inbox permission_handler

The permission_handle package allows us to easily code for user permission to read sms in flutter.

2 Adding Permissions to Android’s Manifest File

Before we start any flutter coding, we need to first add permission statements in the android manifest file to let the system know that the sms feature will be required. Here are the two lines of code to add to your AndroidManifest.xml file usually found here android\app\src\main\AndroidManifest.xml

<uses-feature android:name="android.hardware.telephony" android:required="false" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

3. Coding for user permission

Now let’s get into the flutter code. You might want to have a button or some sort of UI widget to trigger the permission request. Here is what you’d probably have.

import 'package:flutter/material.dart';
import 'package:flutter_sms_inbox/flutter_sms_inbox.dart';
import 'package:permission_handler/permission_handler.dart';

class SmsPage extends StatefulWidget {
const SmsPage({Key? key}) : super(key: key);

@override
State<SmsPage> createState() => _SmsPageState();
}

class _SmsPageState extends State<SmsPage> {

final SmsQuery _query = SmsQuery();

void _askForPermission() async {}

void _readSMSInbox() async {}

@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
ElevatedButton(
onPressed: _askForPermission,
child: const Text("Ask For Permission"),
),
ElevatedButton(
onPressed: _readSMSInbox,
child: const Text("Read SMSs"),
),
],
),
);
}
}

You what to update the _askForPermission function to first check the sms permission status whether the user has already accepted the permission or not. If the user has not accepted the permission, you’ll call a function that will ask the user for permission.

void _askForPermission() async {
var status = await Permission.sms.status;
if (!status.isGranted) {
var permission = await Permission.sms.request();
if (permission.isGranted) {
// What you want to do after user has granted the permission
}
}
}

4 Coding to Read the SMS

Alright here comes the fun part, actually reading the SMS message. Update the _readSMSInbox function with the follow code.

void _readSMSInbox() async {
final List<SmsMessage> messages = await _query.querySms(
// address: +265XXXXXX, // sms phonenumber/address
kinds: [SmsQueryKind.inbox],
count: 10, // how many you want to read
);

debugPrint(messages[0].body);
}

The querySms function the flutter_sms_inbox package provides us, enables us to specify a number of these when get the sms messages from inbox. We can collect a specific number, get messages sent or messages received and only collect message from a specific address.

Alright with that you have everything you need. It goes without saying but don’t use this for evil, but then again a developer is reading this so you never know.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Articles & Posts