Spaces:
Running

messenger / services /push.js
4send's picture
Upload 73 files
71601b1 verified
Raw History Blame Contribute Delete
651 Bytes
const User = require('../models/User');
const admin = require('../config/firebase');
const sendPushNotification = async (username, message) => {
try {
if (typeof admin.messaging !== 'function') return;
const user = await User.findOne({ username }).lean();
if (!user || !user.pushToken) return;
await admin.messaging().send({ ...message, token: user.pushToken });
} catch (err) {
if (err.code === 'messaging/registration-token-not-registered') {
await User.updateOne({ username }, { $unset: { pushToken: 1 } }).catch(() => {});
}
}
};
module.exports = { sendPushNotification };