Can anyone share sample example for SMS Receiver



Please any one share example for SMS Receiver.


@puttapravinkumar0 Here is how,

Add the following permissions to your AndroidManifest.xml located at App_Resources/Android

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

Create a new file named in your application root and extend android.content.BroadcastReceiver.

class SmsReceiver extends android.content.BroadcastReceiver {

    onReceive(context: android.content.Context, intent: android.content.Intent) {
        console.log("SMS Received");
        try {
            let bundle = intent.getExtras();
            if (bundle != null) {
                let puds = bundle.get("pdus");
                for (let i = 0; i < puds.length; i++) {
                    // do anything with SMS
                    let currentMessage = android.telephony.SmsMessage.createFromPdu(puds[i]);
                    // show toast 
                        `From: ${currentMessage.getDisplayOriginatingAddress()}\nMessage: ${currentMessage.getDisplayMessageBody()}`,
        } catch (e) {
            console.log("Failed to read SMS");

You will have to register your new BroadcastReceiver (SmsReceiver) in your AndroidManifest.xml, inside application tag.

<receiver android:name="me.dcoder.SmsReceiver">   
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />

Note: Package / file names could be of your choice.

In >= Android 6.x you will have to acquire the permissions at runtime, so use nativescript-permissions plugin.

        if (!permissions.hasPermission((<any>android).Manifest.permission.RECEIVE_SMS)) {
            permissions.requestPermission((<any>android).Manifest.permission.RECEIVE_SMS, "Allow us to read your SMS")
                .then(() => {
                    console.log("Got permissions");
                }).catch(() => {
                    console.log("Failed to get permissions");

Now you should see SMS Received in console logs and a toast message with sender number and message, whenever your device receives a SMS.


Awesome. @manojdcoder
Thanks for sharing this :slight_smile:


If you don’t want to keep the sms receiver running, then it is better to create it programmatically.

Start by adding permissions to manifest file.

Then in the loaded method of the page where you want to read sms you can do something like this

// Register sms receiver
    if ( {
        console.log("Starting sms reader");,
            function onReceiveCallback(context, intent) {
                console.log("SMS Received");
                try {
                    const bundle = intent.getExtras();
                    let otp;
                    if (bundle !== null) {
                        const puds = bundle.get("pdus");
                        for (let i = 0; i < puds.length; i++) {
                            // do anything with SMS
                            const currentMessage = android.telephony.SmsMessage.createFromPdu(puds[i]);
                            const body = currentMessage.getDisplayMessageBody() + "";
                            const tmp = body.slice(0, 6);
                            if (tmp.length === 6) {
                                otp = tmp;
                    console.log("OTP was: ", otp);
                } catch (e) {
                    console.log("Failed to read SMS");

And then when you finish reading otp, and also in the unloaded method of the page, you can unregister the receiver

// When no longer needed, unregister the broadcast receiver
    if ( {;

Hope that helps!

Cheers! Happy NativeScripting.


It’s working! ut meybe you can help me with joining that code with angular service? I want to display sms on the list of SMSs. How can I do that?


@multishiv19 - It doesn’t work… :frowning:

android.provider.Telephony… namespace doesn’t exists anymore…


If the namespace doesn’t work for you,
You can directly pass this string there



really? its not working for me …