Title: Getting success/failure Response on android.SmsManager.sendTextMessage() in NativeScript/Android
Tags: android;angular;nativescript
Question: I'm sending an SMS from my NativeScript mobile application and this is the following code:
sendSms(numbers, body) {
let sms = android.telephony.SmsManager.getDefault();
sms.sendTextMessage(numbers, null, body, null, null);
The SMS successfully sends using this bit of code, but I want to get a response if the SMS has successfully sent to display to the user of my application.
Something like if it was a Promise:
sms.sendTextMessage(numbers, null, body, null, null).then(res =&gt; {
if(res=='success') someCode;
})
Any ideas on how to accomplish this?
Thanks.
Comment: I see. Any idea on how would this be implemented in NativeScript's Angular typeography, or just the regular JavaScript in NativeScript?

Comment: You need to use `sentIntent` and `deliveryIntent` - [see this link](https://developer.android.com/reference/android/telephony/SmsManager.html#sendTextMessage) (search that page for sendTextMessage - the direct link won't work here)

Here is the accepted answer: Thanks for the help everyone, I've looked into the PendingIntent android reference and I've written a simple NativeScript x TypeScript x Angular solution to achieve this.
Here it is below in case someone else is looking for it in the future.
import * as app from "tns-core-modules/application";
import * as utils from "utils/utils";
@Injectable()
export class SmsService {
context = utils.ad.getApplicationContext();
socket;
constructor() {
sendSms(numbers, body) {
let id = 'messageSent';
let pendingIntent = this.pendingIntent(id);
let sms = android.telephony.SmsManager.getDefault();
sms.sendTextMessage(numbers, null, body, pendingIntent, null);
this.broadcastReciever(id, () =&gt; {
console.log('This should call after sent.');
})
broadcastReciever(id, cb) {
app.android.registerBroadcastReceiver(id, () =&gt; {
cb();
});
pendingIntent(id) {
let intent = new android.content.Intent(id);
return android.app.PendingIntent.getBroadcast(this.context, 0, intent, 0);
Quick edit: in the broadcastReciever() function in the callback you have to unregister the Receiver, because it will register new Receivers every time you send an SMS, so this is what it looks like now:
sendSms(numbers, body) {
let sms = android.telephony.SmsManager.getDefault();
sms.sendTextMessage(numbers, null, body, this.pendingIntentSent, null);
this.broadcastReciever(this.id, () =&gt; {
console.log('@@@ SUCCESFULLY SENT @@@');
app.android.unregisterBroadcastReceiver(this.id);
})

Here is another answer: As Archer suggested, use PendingIntents to be notified when the sms has been sent, and/or delivered.
PendingIntent.getBroadcast() - will retrieve a PI instance.
var pISent = android.app.PendingIntent.getBroadcast(context, 0, new android.content.Intent(android.telephony.SmsManager.STATUS_ON_ICC_SENT), 0);
sms.sendTextMessage(numbers, null, body, pISent, null);
Read more on Pending Intents on the Android Docs to gain better understanding of how Android works - https://developer.android.com/reference/android/app/PendingIntent.html

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:02/07/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><angular><nativescript>', 'Title': 'Getting success/failure Response on android.SmsManager.sendTextMessage() in NativeScript/Android', 'ViewCount': 823, 'id': 48662666, 'user_AboutMe': '<p>Apart from programming, I play the violin.</p>\n', 'user_AccountId': 10057618.0, 'user_DisplayName': 'filipbarak', 'user_Reputation': 1797.0, 'user_Views': 209.0, 'user_id': 7435344.0}}