发送按钮中的发送值单击警报控制中的事件

时间:2018-03-26 06:51:07

标签: javascript ajax firebase firebase-realtime-database ionic3

我想在我的应用程序中创建一个简单的恐慌按钮。这个按钮将按以下方式工作:

  

如果用户点击恐慌按钮,将在此警报中显示一个警报   将向用户提供他想要发送的预定义组   恐慌,

1) if user select committee group then user panic message will
   only sent to user who have role committee. 
2) if user select All then
   panic message will sent to all the member of application.

我不知道如何将该特定值发送到按钮点击事件。这里groupList包含我的所有组名。 这是我的代码,

     sendTextMessage() {

    let Groups: any = [];
    firebase.database().ref('panic_group').orderByKey().once('value', (items: any) => {

      items.forEach((item) => {
        Groups.push(item.val().Group_name);
        this.groupList = Groups;
      });

    let alertConfirm = this.alertCtrl.create();
    alertConfirm.setTitle('Choose Group');

    // Add the new group here!

    this.groupList.forEach(group => {
      alertConfirm.addInput({
            type: 'radio',
            label: group,
            value: group.toLowerCase(),
            checked: group === group.toLowerCase()
        });
    });

    // ...
    alertConfirm.addButton({
            text: 'Cancel',
            role: 'cancel',
            handler: () => {
              console.log("Cancel clicked");
            }
    });
    alertConfirm.addButton({
      text: 'Send',
          handler: () => {
            console.log("Send clicked");
           alert('cnt' + this.contacts);

            var request = new XMLHttpRequest();
            request.open("POST", "https://control.msg91.com", true);
            request.setRequestHeader("Access-Control-Allow-Origin", "*");

            var settings = {
              "async": true,
              "crossDomain": true,
              "url": "http://api.msg91.com/api/v2/sendsms",
              "method": "POST",
              "headers": {
                "authkey": "198608A17R3Rqgwdh5a87f2ae",
                "content-type": "application/json"
              },
              "processData": false,
              "data": "{ \"sender\": \"SOCKET\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"Panic Alert from Flat No " + this.flatNo + "\", \"to\": [ " + this.contacts + " ] }, { \"message\": \"Test Panic Alert from Flat No " + this.flatNo + "\", \"to\": [ " + this.contacts + " ] } ] }"


            }
            alert(settings.data);
            console.log(settings.data);
            jQuery.ajax(settings).done(function (response) {
              console.log(response);
              alert('res' + response);
              alert("Sms Sent!");
            });
          }
     });

    alertConfirm.present();
    });
  }

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

loadContacts(userGroup : string) {

 let contacts: any = [];

if(userGroup === 'Commitee Member'){

   var ref = firebase.database().ref("users");
   ref.orderByChild("role").equalTo(userGroup).once("value", (items: any) => {

    items.forEach((item) => {
      if (item.val().contact_no != '0') {
        contacts.push({
          cnt:item.val().contact_no,
          flatNo:item.val().flatno
        });
      }
      this.contactList = contacts;

    });

    var length = this.contactList.length;

    if (length > 0) {
      for (var i = 0; i < length; i++) {

          this.contacts.push('<SMS TEXT=\"Panic  dynamic Alert for Commiittee members from Flat No '+this.contactList[i].flatNo + '\"><ADDRESS TO=\"'+this.contactList[i].cnt +'\"></ADDRESS></SMS>');
      }

      console.log("Formatted Contacts: ", this.contacts);
    }
  },
  (error) => {
    console.log("Error: ", error);
  });
}else{
  console.log(userGroup +"else");
  firebase.database().ref('users').orderByKey().once('value', (items: any) => {

    items.forEach((item) => {

      if (item.val().contact_no != '0') {
        contacts.push({
          cnt:item.val().contact_no,
          flatNo:item.val().flatno
        });
      }
      this.contactList = contacts;
    });

    var length = this.contactList.length;
    console.log(length);
    if (length > 0) {
      for (var i = 0; i < length; i++) {

         this.contacts.push('<SMS TEXT=\"Panic  dynamic Alert for All members from Flat No '+this.contactList[i].flatNo + '\"><ADDRESS TO=\"'+this.contactList[i].cnt +'\"></ADDRESS></SMS>');
      }

      console.log("Formatted Contacts: ", this.contacts);
    }
  },
    (error) => {
      console.log("Error: ", error);
    });
}

}

alertConfirm.addButton({
  text: 'Send',
      handler:  data  => {
        console.log("Send clicked : "+data);
        this.loadContacts(data);
        var request = new XMLHttpRequest();
        request.open("POST", "https://control.msg91.com", true);

        var settings = {
          "async": true,
          "crossDomain": true,
          "url": "https://control.msg91.com/api/postsms.php",
          "method": "POST",
          "headers": {
            "content-type": "application/json"
          },
          "data": "<MESSAGE><AUTHKEY>******************</AUTHKEY><SENDER>SQatta</SENDER><ROUTE>4</ROUTE><CAMPAIGN>Panic Alert API</CAMPAIGN><COUNTRY>91</COUNTRY>"+ this.contacts + "</MESSAGE>"
        }

        jQuery.ajax(settings).done(function (response) {
          console.log(response);
          alert("Sms Sent!");
        });

      }
 });