bitcore-wallet-service/lib/pushnotificationsservice.js

96 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-01-04 09:43:21 -08:00
'use strict';
var async = require('async');
var log = require('npmlog');
log.debug = log.verbose;
var request = require('request');
var MessageBroker = require('./messagebroker');
2016-01-04 09:47:57 -08:00
var PUSHNOTIFICATIONS_TYPES = {
2016-01-04 09:43:21 -08:00
'NewCopayer': {
2016-01-05 05:53:05 -08:00
title: "New copayer",
message: function(notification) {
return ("Copayer: " + notification.data.copayerName + " has joined!");
};
2016-01-04 09:43:21 -08:00
},
'WalletComplete': {
2016-01-05 05:53:05 -08:00
title: "Wallet complete",
message: function(notification) {
return ("All copayers has joined!");
};
2016-01-04 09:43:21 -08:00
},
'NewTxProposal': {
2016-01-05 05:53:05 -08:00
title: "New proposal",
message: function(notification) {
return ("New transaction proposal created");
};
2016-01-04 09:43:21 -08:00
},
'NewOutgoingTx': {
2016-01-05 05:53:05 -08:00
title: "New outgoing transaction",
message: function(notification) {
return ((notification.data.amount / 100) + " bits");
};
2016-01-04 09:43:21 -08:00
},
'NewIncomingTx': {
2016-01-05 05:53:05 -08:00
title: "New incoming transaction",
message: function(notification) {
return ((notification.data.amount / 100) + " bits");
};
2016-01-04 09:43:21 -08:00
},
'TxProposalFinallyRejected': {
2016-01-05 05:53:05 -08:00
title: "Rejected",
message: function(notification) {
return ("Transaction proposal finally rejected");
};
2016-01-04 09:43:21 -08:00
},
};
2016-01-04 09:47:57 -08:00
function PushNotificationsService() {};
2016-01-04 09:43:21 -08:00
2016-01-04 09:47:57 -08:00
PushNotificationsService.prototype.start = function(opts, cb) {
2016-01-04 10:56:14 -08:00
opts = opts || {};
2016-01-04 09:43:21 -08:00
var self = this;
2016-01-04 10:56:14 -08:00
2016-01-04 09:43:21 -08:00
async.parallel([
function(done) {
self.messageBroker = opts.messageBroker || new MessageBroker(opts.messageBrokerOpts);
2016-01-04 10:56:14 -08:00
self.messageBroker.onMessage(_.bind(self.sendPushNotifications, self));
2016-01-04 09:43:21 -08:00
done();
},
], function(err) {
if (err) {
log.error(err);
}
return cb(err);
});
};
2016-01-04 09:47:57 -08:00
PushNotificationsService.prototype.sendPushNotifications = function(notification, cb) {
2016-01-05 05:53:05 -08:00
log.debug(notification);
2016-01-04 09:43:21 -08:00
2016-01-05 05:53:05 -08:00
if (!PUSHNOTIFICATIONS_TYPES[notification.type]) return;
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"data": {
"title": PUSHNOTIFICATIONS_TYPES[notification.type].title,
"message": PUSHNOTIFICATIONS_TYPES[notification.type].message(notification)
2016-01-04 09:43:21 -08:00
}
2016-01-05 05:53:05 -08:00
};
var url = 'http://192.168.1.121:8000/send';
request({
url: url,
method: 'POST',
json: true,
body: opts
}, function(error, response, body) {
log.debug(response.statusCode);
});
}
2016-01-04 09:43:21 -08:00
};
2016-01-04 09:47:57 -08:00
module.exports = PushNotificationsService;