Advertisement
sombriks

onersignal api server

Jul 28th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.16 KB | None | 0 0
  1. var Promise = require("bluebird");
  2. var https = require('https');
  3. var util = require('util');
  4.  
  5. var options = {
  6.   host: "onesignal.com",
  7.   port: 443,
  8.   path: "/api/v1/notifications",
  9.   method: "POST",
  10.   headers: {
  11.     "Content-Type": "application/json",
  12.     "Authorization": "xxx"
  13.   }
  14. };
  15.  
  16. module.exports = {
  17.   sendNotification: function (cfg) {
  18.     return new Promise(function (resolve, reject) {
  19.       var data = {
  20.         isIos: true,
  21.         isAndroid: true,
  22.         app_id: cfg.appid,
  23.         include_player_ids: cfg.userids,
  24.         contents: {
  25.           en: cfg.title
  26.         },
  27.         data: cfg
  28.       };
  29.       console.log("Sending:");
  30.       console.log(util.inspect(data));
  31.       var req = https.request(options, function (res) {
  32.         res.on('data', function (data) {
  33.           console.log("Response:");
  34.           console.log(util.inspect(JSON.parse(data), false, null));
  35.           resolve(JSON.parse(data));
  36.         });
  37.       });
  38.       req.on('error', function (e) {
  39.         console.log("ERROR:");
  40.         console.log(util.inspect(e));
  41.         reject(e);
  42.       });
  43.       req.write(JSON.stringify(data));
  44.       req.end();
  45.     });
  46.   }
  47. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement