This repository was archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpush-demo.js
More file actions
117 lines (105 loc) · 3.07 KB
/
push-demo.js
File metadata and controls
117 lines (105 loc) · 3.07 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: loopback-example-push
module.exports = function (app) {
var Notification = app.models.notification;
var Application = app.models.application;
var PushModel = app.models.push;
function startPushServer() {
// Add our custom routes
var badge = 1;
app.post('/notify/:id', function (req, res, next) {
var note = new Notification({
expirationInterval: 3600, // Expires 1 hour from now.
badge: badge++,
sound: 'ping.aiff',
alert: '\uD83D\uDCE7 \u2709 ' + 'Hello',
messageFrom: 'Ray'
});
PushModel.notifyById(req.params.id, note, function (err) {
if (err) {
console.error('Cannot notify %j: %s', req.params.id, err.stack);
next(err);
return;
}
console.log('pushing notification to %j', req.params.id);
res.send(200, 'OK');
});
});
PushModel.on('error', function (err) {
console.error('Push Notification error: ', err.stack);
});
// Pre-register an application that is ready to be used for testing.
// You should tweak config options in ./config.js
var config = require('./config');
var demoApp = {
id: 'loopback-component-push-app',
userId: 'strongloop',
name: config.appName,
description: 'LoopBack Push Notification Demo Application',
pushSettings: {
apns: {
certData: config.apnsCertData,
keyData: config.apnsKeyData,
pushOptions: {
// Extra options can go here for APN
},
feedbackOptions: {
batchFeedback: true,
interval: 300
}
},
gcm: {
serverApiKey: config.gcmServerApiKey
}
}
};
updateOrCreateApp(function (err, appModel) {
if (err) {
throw err;
}
console.log('Application id: %j', appModel.id);
});
//--- Helper functions ---
function updateOrCreateApp(cb) {
Application.findOne({
where: { id: demoApp.id }
},
function (err, result) {
if (err) cb(err);
if (result) {
console.log('Updating application: ' + result.id);
delete demoApp.id;
result.updateAttributes(demoApp, cb);
} else {
return registerApp(cb);
}
});
}
function registerApp(cb) {
console.log('Registering a new Application...');
// Hack to set the app id to a fixed value so that we don't have to change
// the client settings
Application.beforeSave = function (next) {
if (this.name === demoApp.name) {
this.id = 'loopback-component-push-app';
}
next();
};
Application.register(
demoApp.userId,
demoApp.name,
{
description: demoApp.description,
pushSettings: demoApp.pushSettings
},
function (err, app) {
if (err) {
return cb(err);
}
return cb(null, app);
}
);
}
}
startPushServer();
};