Angular/Ionic4 如何在不打开原生短信应用程序的情况下发送短信

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

所以我一直在关注 Ionic Native SMS 插件的 github repo(https://github.com/cordova-sms/cordova-sms-plugin), 我已经配置了 repo 建议:

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
            }
        };

但是,当我在真实设备上测试时,它仍然不发送短信。

任何人都可以帮助我,我需要添加权限吗? 这是我到目前为止的代码

 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');
    });
  }

您不是在发送短信,您是在创建 INTENT 来发送短信。

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.

这意味着代码仅生成 'intent',然后将其传递给您的 phone 的默认应用,由后者处理。它如何处理 intent 仅取决于应用程序。甚至可以有多个应用程序可以处理 intent,然后用户会得到一个选择对话框。当短信真正发送时,您的应用无法控制。

这实际上是一件好事,因此如果您安装任何应用程序,您可以确定它不会向订阅服务发送短信,并且您需要支付 100 欧元 phone 的账单。

您无需打开本机短信应用即可发送短信。 您需要使用 Android 权限才能获得短信权限

使用这两个函数

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]);
}

并且您还需要在 Android 清单中包含这些函数。

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

然后是短信功能本身

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);
  });
}