简体   繁体   English

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

[英]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:因此,我一直在关注 Ionic Native SMS 插件的 github 回购( https://github.com/cordova-sms/cordova-sms-plugin ),并且我已经按照回购建议进行了配置:

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您需要使用 Android 权限来获取短信权限

use these two function使用这两个 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.您还需要在 Android 清单中包含这些功能。

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

and then the SMS function itself然后是短信 function 本身

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.您不是在发送 SMS,而是在创建发送 SMS 的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. Intent 允许您通过在 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.它如何处理intent仅取决于应用程序。 There can even be multiple apps which can handle the intent , then the use gets a choose dialog.甚至可以有多个应用程序可以处理intent ,然后用户会得到一个选择对话框。 Your app has no control, when the SMS really gets send.当 SMS 真正发送时,您的应用程序无法控制。

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.这实际上是一件好事,因此,如果您安装任何应用程序,您可以确定它不会向订阅服务发送 SMS,并且您需要支付 100 欧元的电话费。

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.那里有很多,例如 Twilio 是一个受欢迎的提供商。

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.我认为您需要使用服务器端技术或通过云 function发送它,以便您可以控制它的安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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