Spaces:
Running

File size: 651 Bytes
71601b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 };