How to work properly with the request?

Asked

Viewed 46 times

0

I need to send a simple notification using onesignal, but is giving error in the build of the web app because of https import, as the code I am importing as follows:

var https = require('https');

Note: This example is from the site of onesignal itself, the problem is that I can not do the request otherwise, so it works, but at the time of the build error. So what I need to basically do is, figure out another way to call this https to request the onesignal api and solve the problem. That’s all I need to close this project.

  notificaAgendamentoCriadoProfissional(){
  var sendNotification = function(data) {
  var headers = {
    "Content-Type": "application/json; charset=utf-8"
  };

  var options = {
    host: "onesignal.com",
    port: 443,
    path: "/api/v1/notifications",
    method: "POST",
    headers: headers
  };

  var https = require('https');
  var req = https.request(options, function(res) {  
    res.on('data', function(data) {
      console.log("Response:");
      console.log(JSON.parse(data));
    });
  });

  req.on('error', function(e) {
    console.log("ERROR:");
    console.log(e);
  });

  req.write(JSON.stringify(data));
  req.end();
};

var message = { 
  app_id: "meu id",
  contents: {"en": "Você recebeu uma nova solicitação. Verifique imediatamente a sua lista de agendamentos"},
  include_player_ids: [this.idOneSignalProfissional]
};

sendNotification(message);

}

The mistake I’m getting is always this:

[18:23:19]  typescript: ...roAdm/src/pages/criar-agendamento-procedimento/criar-agendamento-procedimento.ts, line: 178 
        Cannot find name 'require'.

L178:   var https = require('https');
  • I solved here, I was able to reassemble the function and do the post!

  • 4

    Diego, consider posting an answer showing what was wrong and how you managed to solve it.

  • I updated, and put the example of the correction

1 answer

0

Here’s an example of how I solved this question:

  notificaAgendamentoCriadoProfissional() {
  let data = {
  app_id: "Seu Id do onesignal",
  contents: { "en": "Você recebeu uma nova solicitação. Verifique imediatamente a sua lista de agendamentos" },
  include_player_ids: ['id do usuário do one signal']
};
let url = 'https://onesignal.com/api/v1/notifications';
let headers = new HttpHeaders({
  "Content-Type": "application/json;charset=utf-8"
});

this.http.post(url, data, { headers: headers })
  .subscribe((result: any) => {
    console.log('sucesso ao salvar');
    console.log(result);
  },
    (error) => {
      console.log(error);
    });

}

  • If this has solved your problem, as soon as you can accept your answer (see green).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.