-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (34 loc) · 1.12 KB
/
server.js
File metadata and controls
42 lines (34 loc) · 1.12 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
const admin = require('firebase-admin');
const express = require('express');
const bodyParser = require('body-parser');
// Initialize Firebase Admin SDK
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const app = express();
app.use(bodyParser.json());
app.post('/send-notification', async (req, res) => {
const { token, title, body } = req.body;
if (!token || !title || !body) {
return res.status(400).send('Token, title, and body are required');
}
const message = {
notification: {
title: title,
body: body,
},
token: token,
};
try {
const response = await admin.messaging().send(message);
console.log('Successfully sent message:', response);
res.status(200).send('Notification sent successfully');
} catch (error) {
console.error('Error sending message:', error);
res.status(500).send('Error sending notification');
}
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});