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