-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification-example.js
More file actions
148 lines (137 loc) · 3.78 KB
/
notification-example.js
File metadata and controls
148 lines (137 loc) · 3.78 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const request = require('request');
module.exports = {
inputs: {
to: {
type: 'string',
required: true,
},
title: {
type: 'string',
required: true,
},
body: {
type: 'string',
required: true,
},
content: {
type: 'string',
required: true,
},
type: {
type: 'string',
required: true,
},
},
exits: {
success: {},
},
fn: async function(inputs, exits) {
const headers = {
'Content-Type': 'application/json',
Authorization: `key=${sails.config.firebaseServerKey}`,
};
let options = {};
if (inputs.type === 'Android') {
options = {
url: 'https://fcm.googleapis.com/fcm/send',
method: 'POST',
headers: headers,
json: {
data: {
title: inputs.title,
body: inputs.body,
content: inputs.content,
},
registration_ids: [inputs.to],
},
};
} else if (inputs.type === 'IOS') {
options = {
url: 'https://fcm.googleapis.com/fcm/send',
method: 'POST',
headers: headers,
json: {
notification: {
title: inputs.title,
body: inputs.body,
sound: 'default',
content: inputs.content,
},
to: inputs.to,
},
};
}
request(options, async (error, response, body) => {
if (!error && response.statusCode === 200) {
console.log(body);
return exits.success(body);
} else {
console.log(error);
return exits.success(error);
}
});
},
};
//----------- enother example
module.exports = class FcmService {
constructor(fcm_server_key, datasource) {
var FCM = require('fcm-node');
this.fcm = new FCM(fcm_server_key);
this.datasource = datasource;
}
sendFcmMessage(recipient, collapse_key, title, body, data) {
let thisContext = this;
let registration_token = recipient.pushtoken;
return new Promise(function(resolve, reject) {
let message = {
// this may vary according to the message type (single recipient, multicast, topic, et cetera)
to: registration_token,
collapse_key: collapse_key,
priority: 'high',
content_available: true,
data: data,
};
let collection = thisContext.datasource.connector.collection('Notification');
collection.aggregate(
[
{
$match: {
$and: [{ ownerUserId: recipient.id }, { unread: true }, { type: { $ne: 'meet' } }],
},
},
{ $group: { _id: '$ownerUserId', total: { $sum: 1 } } },
],
function(err, rows) {
let unread = 0;
if (!err && rows.length > 0) {
unread = rows[0].total;
}
if (title && body && !recipient.isAndroid) {
message.notification = {
title: title,
body: body,
badge: unread,
sound: 'notification.wav',
};
} else if (title && body && recipient.isAndroid) {
message.data.notification_title = title;
message.data.notification_body = body;
message.data.notification_badge = unread;
message.data.notification_sound = 'notification.wav';
}
console.log(`FCM notify ${recipient.name} of ${title} with badge ${unread}`);
thisContext.fcm.send(message, function(err, response) {
if (err) {
console.log('Something has gone wrong!');
console.log(err);
reject(err);
} else {
console.log('Successfully sent with response: ', response);
resolve(response);
}
});
},
);
});
}
};