Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #3 from julianusti/master
Browse files Browse the repository at this point in the history
add Android push notification
  • Loading branch information
alexbeletsky committed Aug 13, 2014
2 parents 16537bd + e9357d8 commit aa8df69
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ Each `.execute()` function callback receives `transport` object, which exposes i
Supported now:
* [Mandrill](https://github.com/jimrubenstein/node-mandrill)
* [Twillio](https://github.com/twilio/twilio-node)
* [Android push notification](https://github.com/ToothlessGear/node-gcm)

Will be added soon:
* [Mailgun]()
* [iPhone / Android push notifications]()
* [Mailgun](https://github.com/jimrubenstein/node-mandrill)
* [iOS push notification](https://github.com/argon/node-apn)

If you want to extend transport support:

Expand Down
3 changes: 3 additions & 0 deletions config/development.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var config = {
twilio : {
accountSid: 'fake-twilio-account-sid',
authToken: 'fake-twilio-auth-token'
},
gcm : {
serverApiKey: 'fake-google-server-api-key'
}
},

Expand Down
3 changes: 3 additions & 0 deletions config/production.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var config = {
twilio : {
accountSid: process.env.TWILIO_ACCOUNT_SID,
authToken: process.env.TWILIO_ACCOUNT_TOKEN
},
gcm : {
serverApiKey: process.env.GOOGLE_SERVER_API_KEY
}
},

Expand Down
3 changes: 3 additions & 0 deletions config/staging.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var config = {
twilio : {
accountSid: process.env.TWILIO_ACCOUNT_SID,
authToken: process.env.TWILIO_ACCOUNT_TOKEN
},
gcm : {
serverApiKey: process.env.GOOGLE_SERVER_API_KEY
}
},

Expand Down
3 changes: 3 additions & 0 deletions config/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var config = {
twilio : {
accountSid: 'fake-twilio-account-sid',
authToken: 'fake-twilio-auth-token'
},
gcm : {
serverApiKey: 'fake-google-server-api-key'
}
},

Expand Down
31 changes: 29 additions & 2 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ notifier
return callback(err);
}

actions.resolved(a, {phone: user.email}, callback);
actions.resolved(a, {phone: user.phone}, callback);
});
}).
execute('send-verify-sms', function (a, transport, callback) {
Expand All @@ -54,13 +54,40 @@ notifier
}, callback);
});

notifier
.receive('user-completed-action', function (e, actions, callback) {
actions.create('send-android-push-notification', {user: e.user}, callback);
})
.resolve('send-android-push-notification', function (a, actions, callback) {
asyncRequestForUser(actions.user, function (err, user) {
if (err) {
return callback(err);
}

actions.resolved(a, {deviceId: user.deviceId}, callback);
});
})
.execute('send-android-push-notification', function (a, transport, callback) {
var registrationIds = [];
registrationIds.push(a.data.deviceId);

var message = transport.android.message;
message.addDataWithObject({
key1: 'message1',
key2: 'message2'
});

transport.android.push.send(message, registrationIds, 4, callback);
});

notifier.start(process.env.NODE_PORT || 3031);

function asyncRequestForUser(userId, callback) {
var user = {
email: 'example@likeastore.com',
name: 'alexander.beletsky',
phone: '+3805554455'
phone: '+3805554455',
deviceId: 'regId123'
};

process.nextTick(function () {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
"homepage": "https://github.com/likeastore/notifier",
"dependencies": {
"agenda": "~0.6.4",
"apn": "^1.6.0",
"async": "~0.7.0",
"colors": "~0.6.2",
"express": "~3.5.1",
"moment": "~2.6.0",
"mongojs": "~0.11.0",
"node-gcm": "^0.9.12",
"node-logentries": "~0.1.4",
"node-mandrill": "~1.0.1",
"postal": "~0.9.0-rc1",
Expand Down
19 changes: 18 additions & 1 deletion source/transport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var mandrill = require('node-mandrill');
var twilio = require('twilio');
var gcm = require('node-gcm');
var config = require('../config');

var setupMandrill = function () {
Expand All @@ -26,9 +27,25 @@ var setupTwillio = function () {
}
};

var setupAndroidPushNotification = function () {
if(!validConfig()) {
throw new Error('missing server api key, please update config.transport.gcm.serverApiKey section');
}

return {
push: new gcm.Sender(config.transport.gcm.serverApiKey),
message: new gcm.Message()
};

function validConfig() {
return config.transport.gcm && config.transport.gcm.serverApiKey;
}
};

var transport = {
mandrill: setupMandrill(),
twillio: setupTwillio()
twillio: setupTwillio(),
android: setupAndroidPushNotification()
};

module.exports = transport;

0 comments on commit aa8df69

Please sign in to comment.