From c423b0da44a0e460f32d4374ec8a8623e52c4e77 Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Tue, 8 Nov 2022 14:41:39 +0100 Subject: [PATCH] added topic to messages --- backend/index.js | 26 ++++++++++++++++++++++++++ frontend/app.vue | 21 ++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/backend/index.js b/backend/index.js index 03c1c07..09ae64f 100644 --- a/backend/index.js +++ b/backend/index.js @@ -2,6 +2,7 @@ const express = require('express'); const cors = require('cors'); const { initializeApp, applicationDefault, cert } = require('firebase-admin/app'); const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore'); +const { getMessaging } = require('firebase-admin/messaging') const { verifyIdToken } = require('firebase-admin/auth') const serviceAccount = require('./firebase.json'); @@ -80,6 +81,31 @@ app.post('/getrelatiecodes', async (req, res) => { res.status(200).send({ code: 'success', relatiecodes: relatiecodes, persons: persons }) + } catch (e) { + return res.status(500).send({ code: 'error', error: e }) + } +}) + +app.post('/subscribetotopic', async (req, res) => { + const { topic, registrationToken } = req.body; + + if (!topic) return res.status(400).send({ code: 'no-topic'}) + if (!registrationToken) return res.status(400).send({ code: 'no-registrationToken'}) + + try { + getMessaging().subscribeToTopic([registrationToken], topic) + .then((response) => { + console.log('Successfully subscribed to topic:', response); + + res.status(200).send({ code: 'success' }) + }) + .catch((error) => { + console.log('Error subscribing to topic:', error); + + return res.status(500).send({ code: 'error', error: e }) + }); + + } catch (e) { return res.status(500).send({ code: 'error', error: e }) } diff --git a/frontend/app.vue b/frontend/app.vue index 2dd7d6a..e87a32b 100644 --- a/frontend/app.vue +++ b/frontend/app.vue @@ -18,6 +18,7 @@