forked from ltebean/node-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.js
60 lines (55 loc) · 1.47 KB
/
notification.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
49
50
51
52
53
54
55
56
57
58
59
60
var apns = require('apns'), notification, options, connection;
var db = require('./db.js').sharedDB;
var Step = require('step');
// options = {
// keyFile : "conf/dev/key.pem",
// certFile : "conf/dev/cert.pem",
// gateway:'gateway.sandbox.push.apple.com',
// debug : true,
// };
options = {
keyFile : "conf/product/key.pem",
certFile : "conf/product/cert.pem",
gateway:'gateway.push.apple.com',
debug : false,
};
var connection = new apns.Connection(options);
exports.send=function(weiboIds,msg,payload){
Step(
function getCollection(){
db.collection('apns', this);
},
function findResult(err,collection){
if (err) throw err;
collection.find({'user.weiboId':{$in:weiboIds}}).toArray(this);
},
function generateResponse(err, results){
if(err) throw err;
results.forEach(function(result){
notification = new apns.Notification();
notification.payload = payload;
notification.badge = 1;
notification.alert = msg;
notification.device = new apns.Device(result.deviceToken);
connection.sendNotification(notification);
})
});
}
exports.register=function(req,res){
Step(
function getCollection(){
db.collection('apns', this);
},
function insertData(err,collection){
if (err) throw err;
collection.update(
{'user.weiboId':req.body.user.weiboId},
req.body,
{safe:true,new:true,upsert:true},
this);
},
function generateResponse(err, result){
if (err) throw err;
res.send(result);
});
}