簡體   English   中英

Angular / Ionic4如何在不打開本機短信應用程序的情況下發送短信

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

因此,我一直在關注 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
            }
        };

但是,當我在真實設備上測試它時,它仍然不發送短信。

誰能幫助我,我需要添加權限嗎? 這是我到目前為止的代碼

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

您可以在不打開本機短信應用程序的情況下發送短信。 您需要使用 Android 權限來獲取短信權限

使用這兩個 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]);
}

您還需要在 Android 清單中包含這些功能。

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

然后是短信 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);
  });
}

您不是在發送 SMS,而是在創建發送 SMS 的INTENT

Intent 允許您通過在 Intent object 中描述您想要執行的簡單操作(例如“查看地圖”或“拍照”)來啟動另一個應用程序中的活動。 這種類型的意圖稱為隱式意圖,因為它不指定要啟動的應用程序組件,而是指定一個操作並提供一些數據來執行該操作。

這意味着代碼僅生成“意圖”,然后將其傳遞給您的手機默認應用程序,由其處理。 它如何處理intent僅取決於應用程序。 甚至可以有多個應用程序可以處理intent ,然后用戶會得到一個選擇對話框。 當 SMS 真正發送時,您的應用程序無法控制。

這實際上是一件好事,因此,如果您安裝任何應用程序,您可以確定它不會向訂閱服務發送 SMS,並且您需要支付 100 歐元的電話費。

在不打開本機應用程序的情況下發送短信的唯一方法是使用付費的第三方服務為您發送短信。

那里有很多,例如 Twilio 是一個受歡迎的提供商。

我認為您需要使用服務器端技術或通過雲 function發送它,以便您可以控制它的安全性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM