簡體   English   中英

離子/角度發送短信到數組中的多個號碼

[英]Ionic/Angular send SMS to multiple numbers from array

我有一個項目,我想向我擁有的聯系人列表發送短信。 不幸的是,我不知道該怎么做,而且我嘗試的一切似乎都不起作用。

我想遍歷數組中的所有號碼,然后使用 IONIC SMS 插件向這些號碼發送短信。

這是存儲該聯系人的組件

constructor(private contacts: Contacts, private storage: Storage) {
    this.getContact();
  }
  key = 'contacts';
  mContacts = [
   {
    id: [0],
    name: '',
    number: ''
   },
   {
    id: [1],
    name : [''],
    number: ['']
   },
   {
    id: [2],
    name : [''],
    number: ['']
   },
   {
    id: [3],
    name : [''],
    number: ['']
   },
   {
    id: [4],
    name : [''],
    number: ['']
   }
  ];

  ngOnInit() {}

  pickContact(contactId) {
    this.contacts.pickContact().then((contact) => {
      this.mContacts[contactId].name = contact.name.givenName;
      this.mContacts[contactId].number = contact.phoneNumbers[0].value;
      this.saveContacts();
    });
  }
  saveContacts() {
    this.storage.set(this.key, JSON.stringify(this.mContacts));
  }
  getContact() {
    this.storage.get(this.key).then((val) => {
      console.log('Contact Name: ', val);
      if (val != null && val !== undefined) {
        this.mContacts = JSON.parse(val);
      }
    });
  }
}

這是發送短信的 function。 它以 getContacts() 方法開始,因為聯系人最初是使用 IONIC 存儲插件存儲的

編輯

我想從聯系人組件中檢索數組列表的內容,但是當我調用 function sendSMS() 它返回一個 null 的號碼

sendSMS()
{
  this.contactComponent.getContact();
  let number = this.contactComponent.mContacts[0][2];

  let message = "Hello World";
  console.log("number=" + number + ", message= " + message);

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



  this.sms.send(number, message, options);
}

希望這可以幫助某人。

我決定這樣做是為了讓事情變得更簡單。

 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,numberTwo ,numberThree,numberFour,numberFive, message, options).then(() => {
    this.presentAlert('Success', 'message has been sent');
  })
  .catch(error => {
    this.presentAlert('Error', 'Failed: ' + error);
  });
}

暫無
暫無

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

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