Node push notification
From DreamFactory
Toddappleton (Talk | contribs) |
Toddappleton (Talk | contribs) |
||
Line 16: | Line 16: | ||
var lodash = require("lodash"); | var lodash = require("lodash"); | ||
var request = require("request"); | var request = require("request"); | ||
− | + | ||
if (event.request.payload.resource) { | if (event.request.payload.resource) { | ||
− | + | ||
lodash._.each(event.request.payload.resource, function( record ) { | lodash._.each(event.request.payload.resource, function( record ) { | ||
− | + | ||
var msg = { | var msg = { | ||
"Message": { | "Message": { | ||
Line 27: | Line 27: | ||
"Subject": "New Todo Created" | "Subject": "New Todo Created" | ||
}; | }; | ||
− | + | ||
var options = { | var options = { | ||
− | // full url for push service on your DreamFactory instance | + | // full url for push service topic on your DreamFactory instance |
− | url: 'http://localhost:8888/api/v2/push/topic/ | + | url: 'http://localhost:8888/api/v2/push/topic/arn:aws:sns:us-east-1:805598006201:test123', |
method: 'POST', | method: 'POST', | ||
json: msg, | json: msg, | ||
Line 39: | Line 39: | ||
} | } | ||
}; | }; | ||
− | + | ||
request(options, function (error, response, body) { | request(options, function (error, response, body) { | ||
if(error) { | if(error) { |
Revision as of 21:12, 7 March 2016
This script uses a pre-configured push notification service to send notifications when new records are created.
// To enable Node.js scripting, set the path to node in your DreamFactory .env file. // This setting is commented out by default. // // DF_NODEJS_PATH=/usr/local/bin/node // // Use npm to install any dependencies. This script requires 'lodash' and 'request'. // Your scripts can call console.log to dump info to the log file in storage/logs. // POST /api/v2/db/_table/todo triggers script db._table.todo.post.post_process // This script runs AFTER records are written to the db. // records are in array event.request.payload.resource. var lodash = require("lodash"); var request = require("request"); if (event.request.payload.resource) { lodash._.each(event.request.payload.resource, function( record ) { var msg = { "Message": { "default": "A new Todo named '" + record.name + "' was just created!" }, "Subject": "New Todo Created" }; var options = { // full url for push service topic on your DreamFactory instance url: 'http://localhost:8888/api/v2/push/topic/arn:aws:sns:us-east-1:805598006201:test123', method: 'POST', json: msg, // Use same auth as user calling this script. headers: { 'x-dreamfactory-api-key': platform.session.api_key, 'x-dreamfactory-session-token': platform.session.session_token } }; request(options, function (error, response, body) { if(error) { console.log(error); } else { console.log(response.statusCode, body); } }); }); }