diff --git a/README.md b/README.md index 9151f7a..e471940 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/config/development.config.js b/config/development.config.js index c79ee5d..366fe17 100644 --- a/config/development.config.js +++ b/config/development.config.js @@ -13,6 +13,9 @@ var config = { twilio : { accountSid: 'fake-twilio-account-sid', authToken: 'fake-twilio-auth-token' + }, + gcm : { + serverApiKey: 'fake-google-server-api-key' } }, diff --git a/config/production.config.js b/config/production.config.js index d34a794..9f3d516 100644 --- a/config/production.config.js +++ b/config/production.config.js @@ -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 } }, diff --git a/config/staging.config.js b/config/staging.config.js index 697beb7..350fb0a 100644 --- a/config/staging.config.js +++ b/config/staging.config.js @@ -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 } }, diff --git a/config/test.config.js b/config/test.config.js index b48a5ec..354e575 100644 --- a/config/test.config.js +++ b/config/test.config.js @@ -13,6 +13,9 @@ var config = { twilio : { accountSid: 'fake-twilio-account-sid', authToken: 'fake-twilio-auth-token' + }, + gcm : { + serverApiKey: 'fake-google-server-api-key' } }, diff --git a/example/server.js b/example/server.js index ba5710a..bb740b0 100644 --- a/example/server.js +++ b/example/server.js @@ -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) { @@ -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 () { diff --git a/package.json b/package.json index db2a32d..08e3db2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/source/transport.js b/source/transport.js index 2d38ecc..205eb02 100644 --- a/source/transport.js +++ b/source/transport.js @@ -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 () { @@ -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; \ No newline at end of file