39 lines
1,006 B
JavaScript
39 lines
1,006 B
JavaScript
const express = require('express');
|
|
const fetch = require('node-fetch');
|
|
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
app.post('/send-notification', async (req, res) => {
|
|
const { expoPushToken, title, body, data } = req.body;
|
|
if (!expoPushToken) {
|
|
return res.status(400).json({ error: 'expoPushToken is required' });
|
|
}
|
|
try {
|
|
const response = await fetch('https://exp.host/--/api/v2/push/send', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Accept-Encoding': 'gzip, deflate',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
to: expoPushToken,
|
|
sound: 'default',
|
|
title,
|
|
body,
|
|
data,
|
|
}),
|
|
});
|
|
const result = await response.json();
|
|
res.json(result);
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
});
|
|
|
|
const PORT = process.env.PORT || 3001;
|
|
app.listen(PORT, () => {
|
|
console.log(`Notification server running on port ${PORT}`);
|
|
});
|