Please any one share example for SMS Receiver.
Can anyone share sample 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 sms-receiver.android.ts
in your application root and extend android.content.BroadcastReceiver
.
@JavaProxy("me.dcoder.SmsReceiver")
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
android.widget.Toast.makeText(
context,
`From: ${currentMessage.getDisplayOriginatingAddress()}\nMessage: ${currentMessage.getDisplayMessageBody()}`,
android.widget.Toast.LENGTH_LONG
).show();
}
}
} 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">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
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.
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 (app.android) {
console.log("Starting sms reader");
app.android.registerBroadcastReceiver(android.provider.Telephony.Sms.Intents.SMS_RECEIVED_ACTION,
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.dir(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 (app.android) {
app.android.unregisterBroadcastReceiver(android.provider.Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
}
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…
android.provider.Telephony… namespace doesn’t exists anymore…
If the namespace doesn’t work for you,
You can directly pass this string there
"android.provider.Telephony.SMS_RECEIVED"
Example
app.android.unregisterBroadcastReceiver("android.provider.Telephony.SMS_RECEIVED");