In our fast-paced world, SMS messages remain a vital means of communication, even with the rise of chat apps.
With Flutter, you can create beautiful, cross-platform applications, and today we’ll explore how to set your Flutter app as the default SMS application on Android. And it will be described the way how to receive the SMS from our app, the first step of the default SMS app.
Our tutorial starts by creating a new Flutter project using the command line:
flutter create sms_app
To begin, it is essential to include the SMS permission in the AndroidManifest.xml file. As you know, in Android access to sensitive resources such as SMS, storage, and media requires explicit user consent.
For the first, add RECEIVE_SMS permission in the manifest section of the AndroidManifest.xml file.
This permission is necessary for our app to receive incoming sms and this prompts Google Play about the permission used in our app.
<uses-permission android:name="android.permission.RECEIVE_SMS" />
What is the next step?
It is essential to register receivers and activities where we can receive sms, handle and compose for sending new sms in our app.
<receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
<receiver android:name=".IncomingSmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!-- BroadcastReceiver that listens for incoming MMS messages -->
<receiver android:name=".MmsReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
<data android:mimeType="application/vnd.wap.mms-message" />
</intent-filter>
</receiver>
<!-- Activity that allows the user to send new SMS/MMS messages -->
<activity android:name=".ComposeSmsActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</activity>
<!-- Service that delivers messages from the phone "quick response" -->
<service android:name=".HeadlessSmsSendService" android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
<data android:scheme="mms" />
<data android:scheme="mmsto" />
</intent-filter>
</service>
Here we registered 2 receivers, 1 activity and 1 service and each of these is for receiving sms, mms and composing new sms.
Now we are at the step of composing MainActivity.kt file to set our app as a default SMS app. This is required because the SMS receiving function is only available if the user sets our app as a default SMS app.
Here we use MethodChannel class of Flutter plugin for Android to ensure communication between dart code and native Android code.
In native Android code, we register the channel where we wrote the kotlin code to set our app as a default SMS app and the dart code calls this channel function.
When the app is opened, the entry point is in the Dart code (main.dart), which calls this method channel to requests Android to prompt the native Android modal asking the user to set the app as the default SMS app.
Finally, we jump to the step of composing the SmsReceiver.kt file to receive SMS in the background. As we configured SmsReceiver class as a receiver intent for the BROADCAST_SMS permission in AndroidManifest.xml file this class inherits BroadcastReceiver class which is a Base class for code that receives and handles broadcast intents sent by Context.sendBroadcast(Intent).
In conclusion, setting up a default SMS app in Flutter is a straightforward process that can greatly enhance the functionality of your mobile application.
By leveraging the available packages and best practices above, you can ensure a seamless and efficient messaging experience for your users.
Thanks for reading!!!
Top comments (0)