|
| 1 | +/* |
| 2 | +** Sample Table Definition - this supports the Azure Mobile Apps |
| 3 | +** TodoItem product |
| 4 | +** https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-cordova-get-started/ |
| 5 | +*/ |
| 6 | +var azureMobileApps = require('azure-mobile-apps'), |
| 7 | +promises = require('azure-mobile-apps/src/utilities/promises'), |
| 8 | +logger = require('azure-mobile-apps/src/logger'); |
| 9 | + |
| 10 | +// Create a new table definition |
| 11 | +var table = azureMobileApps.table(); |
| 12 | + |
| 13 | +// In the TodoItem product, sends a push notification |
| 14 | +// when a new item inserted into the table. |
| 15 | +table.insert(function (context) { |
| 16 | + // For more information about the Notification Hubs JavaScript SDK, |
| 17 | + // see http://aka.ms/nodejshubs |
| 18 | + logger.info('Running TodoItem.insert'); |
| 19 | + |
| 20 | + // Define the push notification template payload. |
| 21 | + // Requires template specified in the client app. See |
| 22 | + // https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-cordova-get-started-push/ |
| 23 | + var payload = '{"message": "' + context.item.text + '" }'; |
| 24 | + |
| 25 | + // Execute the insert. The insert returns the results as a Promise, |
| 26 | + // Do the push as a post-execute action within the promise flow. |
| 27 | + return context.execute() |
| 28 | + .then(function (results) { |
| 29 | + // Only do the push if configured |
| 30 | + if (context.push) { |
| 31 | + |
| 32 | + context.push.send(null, payload, function (error) { |
| 33 | + if (error) { |
| 34 | + logger.error('Error while sending push notification: ', error); |
| 35 | + } else { |
| 36 | + logger.info('Push notification sent successfully!'); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + // Don't forget to return the results from the context.execute() |
| 41 | + return results; |
| 42 | + }) |
| 43 | + .catch(function (error) { |
| 44 | + logger.error('Error while running context.execute: ', error); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +module.exports = table; |
0 commit comments