-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (41 loc) · 1.35 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const admin = require('firebase-admin');
// Path to your service account key file
const serviceAccount = require('./service-account.json');
// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://flux-6e299.firebaseio.com'
});
const app = express();
app.use(bodyParser.json());
// Route to send notification
app.post('/notification', async (req, res) => {
const token = req.headers.authorization.split('Bearer ')[1];
const notification = req.body;
const match = notification.triggerMatch;
const parsed = JSON.parse(match);
const firstMatch = parsed[0];
const title = firstMatch?.Title;
const body = firstMatch?.Description;
console.log({ title, body, token });
const message = {
notification: {
title: title,
body: body
},
token: token
};
try {
const response = await admin.messaging().send(message);
res.status(200).send({ success: true, messageId: response });
} catch (error) {
res.status(500).send({ success: false, error: error.message });
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});