diff --git a/lib/pushnotificationsservice.js b/lib/pushnotificationsservice.js index 3c8990e..4286c37 100644 --- a/lib/pushnotificationsservice.js +++ b/lib/pushnotificationsservice.js @@ -60,6 +60,10 @@ PushNotificationsService.prototype.start = function(opts, cb) { self.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc'; self.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || ''; self.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl; + self.authorizationKey = opts.pushNotificationsOpts.authorizationKey; + + if (!self.authorizationKey) return cb(new Error('Missing authorizationKey attribute in configuration.')); + async.parallel([ function(done) { @@ -117,28 +121,30 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio }, function(contents, next) { async.map(recipientsList, function(recipient, next) { - var opts = {}; var content = contents[recipient.language]; - opts.users = [notification.walletId + '$' + recipient.copayerId]; - opts.android = { - "data": { - "title": content.plain.subject, - "message": content.plain.body, - "walletId": sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(notification.walletId)), - "notId": Math.floor(Math.random() * 100000) + 1 - } - }; - opts.ios = { - "alert": { - "title": content.plain.subject, - "body": content.plain.body - }, - "sound": "default", - "payload": { - "walletId": sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(notification.walletId)) - } - }; - return next(err, opts); + + self.storage.fetchPushNotificationSubs(recipient.copayerId, function(err, subs) { + if (err) return next(err); + + var opts = _.map(subs, function(sub) { + return { + to: sub.token, + priority: 'high', + restricted_package_name: sub.packageName, + notification: { + title: content.plain.subject, + body: content.plain.body, + sound: "default", + click_action: "FCM_PLUGIN_ACTIVITY", + icon: "fcm_push_icon", + }, + data: { + "walletId": sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(notification.walletId)), + }, + }; + }); + return next(err, opts); + }); }, next); }, function(optsList, next) { @@ -356,14 +362,16 @@ PushNotificationsService.prototype._compileTemplate = function(template, extensi PushNotificationsService.prototype._makeRequest = function(opts, cb) { var self = this; - self.request({ + request({ url: self.pushServerUrl + '/send', method: 'POST', json: true, + header: { + 'Content-Type': 'application/json', + 'Authorization': 'key=' + self.authorizationKey, + }, body: opts - }, function(err, response) { - return cb(err, response); - }); + }, cb); }; module.exports = PushNotificationsService;