简体   繁体   中英

Angular/Ionic4 How to send an sms without opening native sms App

So I have been following the github repo( https://github.com/cordova-sms/cordova-sms-plugin ) for the Ionic Native SMS plugin, and I have configured as to the repo suggests with:

var options = {
            replaceLineBreaks: false, // true to replace \n by a new line, false by default
            android: {
                intent: 'INTENT'  // send SMS with the native android SMS messaging
                //intent: '' // send SMS without opening any other app
            }
        };

however, when I test it on a real device, it still does not send a SMS.

can anyone help me, do I need to add in a permission? Here is the code that I have so far

 sendSms() {
    let options = {
      replaceLineBreaks: false, // true to replace \n by a new line, false by default
      android: {
          intent: ''  // send SMS with the native android SMS messaging
          // intent: '' // send SMS without opening any other app
      }
  };
    this.sms.send('656225667', 'SMS Works', options).then(val => {
      alert('It works');
    });
  }

You can send an SMS without opening the native SMS app. you need to use Android Permission to get the SMS Permissions

use these two function

checkSMSPermission() {
  this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.SEND_SMS).then(
    result => console.log('Has permission?', result.hasPermission),
    err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.SEND_SMS)
  );
}
requestSMSPermission() {
  // tslint:disable-next-line: max-line-length
  this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.SEND_SMS, this.androidPermissions.PERMISSION.BROADCAST_SMS]);
}

and you need to include these functions in the Android Manifest aswell.

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

and then the SMS function itself

sendSMS() {
  this.checkSMSPermission();
  this.contactComponent.getContact();
  const numberOne = this.contactComponent.mContacts[0].number;
  const numberTwo = this.contactComponent.mContacts[1].number;
  const numbeThree = this.contactComponent.mContacts[2].number;
  const numberFour = this.contactComponent.mContacts[3].number;
  const numberFive = this.contactComponent.mContacts[4].number;
  console.log(numberOne);

  // tslint:disable-next-line: max-line-length
  const message = this.messageComponent.dangerMessage + ' my location is: lat: ' + this.latitude.toString() + 'lng: ' + this.longitude.toString();
  console.log('number=' + numberOne + ', message= ' + message);

  // CONFIGURATION
  const options = {
      replaceLineBreaks: false, // true to replace \n by a new line, false by default
      android: {
          intent: ''  // send SMS with the native android SMS messaging
          // intent: '' // send SMS without opening any other app
      }
  };
  this.sms.send(numberOne, message, options).then(() => {
    this.presentAlert('Success', 'message has been sent');
  })
  .catch(error => {
    this.presentAlert('Error', 'Failed: ' + error);
  });
}

You are not sending a SMS, you are creating an INTENT to send a SMS.

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object. This type of intent is called an implicit intent because it does not specify the app component to start, but instead specifies an action and provides some data with which to perform the action.

this means the code only generates the 'intent', which is then passed to your phones default app, which handles it. How it handles the intent is only up to the app. There can even be multiple apps which can handle the intent , then the use gets a choose dialog. Your app has no control, when the SMS really gets send.

This is actually a good thing, so if you install any app, you can be sure, that it doesn't send a SMS to a subscription service and you pay 100 € phone bills.

The only way to send SMS without opening the native app is to use a paid 3rd party service to send the SMS for you.

There are many ones out there, for example Twilio is a popular provider.

I think you need to send it using a server side technology or via a cloud function so that you can control the security of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM