Skip to content

Commit

Permalink
Initial push notification functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
melonmanchan committed Jul 21, 2016
1 parent 4e392a8 commit dd30d82
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/routes/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import express from 'express';
import models from '../models';
import { GENERIC, USER } from '../utils/errorTypes.js';
import { verifyJWT, resolveUser } from '../middleware';
import { sendNotification } from '../utils/pushNotifications';

let User = models.User;

Expand All @@ -16,6 +17,9 @@ router.post('/notifications/register', verifyJWT, resolveUser, (req, res, next)
if (token) {
req.user.update({notificationTokens: [ token ]})
.then(updatedUser => {
return sendNotification({priority: 'high', data: { hello: 'world'}, notification: { title: "hallp", body: 'booody'}}, [ token ]);
})
.then(results => {
return res.status(200).json({ message: 'Registered device token succesfully'});
})
.catch(err => {
Expand Down
3 changes: 3 additions & 0 deletions app/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default {
},
rollbar: {
postToken : process.env.ROLLBAR_TOKEN || '',
},
notifications: {
apiToken : process.env.GCM_API_TOKEN || ''
}
}

25 changes: 25 additions & 0 deletions app/utils/pushNotifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

import gcm from 'node-gcm';
import config from './config';

const sender = new gcm.Sender(config.notifications.apiToken);

function sendNotification(contents, tokens) {
return new Promise(function (resolve, reject) {
const message = new gcm.Message(contents);

sender.send(message, { registrationTokens: tokens }, function (err, response) {
if (err) {
console.log(err);
reject(err);
} else {
console.log(response);
resolve(response);
}
});
});
};

export { sendNotification };

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ip": "^1.0.2",
"jsonwebtoken": "^5.5.0",
"lodash": "4.2.1",
"node-gcm": "0.14.3",
"peer": "github:lyxsus/peerjs-server",
"pg": "^4.4.3",
"pg-hstore": "^2.3.2",
Expand Down

0 comments on commit dd30d82

Please sign in to comment.