added topic to messages
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
10856b1709
commit
c423b0da44
@ -2,6 +2,7 @@ const express = require('express');
|
|||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
|
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
|
||||||
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
|
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
|
||||||
|
const { getMessaging } = require('firebase-admin/messaging')
|
||||||
const { verifyIdToken } = require('firebase-admin/auth')
|
const { verifyIdToken } = require('firebase-admin/auth')
|
||||||
|
|
||||||
const serviceAccount = require('./firebase.json');
|
const serviceAccount = require('./firebase.json');
|
||||||
@ -84,3 +85,28 @@ app.post('/getrelatiecodes', async (req, res) => {
|
|||||||
return res.status(500).send({ code: 'error', error: 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 })
|
||||||
|
}
|
||||||
|
})
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { doc, getFirestore, serverTimestamp, writeBatch, collection, getDoc } from "firebase/firestore";
|
import { doc, getFirestore, serverTimestamp, writeBatch, collection, getDoc } from "firebase/firestore";
|
||||||
|
import { FCM } from "@capacitor-community/fcm";
|
||||||
import { getAuth, onAuthStateChanged, signInWithEmailAndPassword } from "firebase/auth";
|
import { getAuth, onAuthStateChanged, signInWithEmailAndPassword } from "firebase/auth";
|
||||||
import { getMessaging, getToken, onMessage } from "firebase/messaging";
|
import { getMessaging, getToken, onMessage } from "firebase/messaging";
|
||||||
import { PushNotifications } from '@capacitor/push-notifications';
|
import { PushNotifications } from '@capacitor/push-notifications';
|
||||||
@ -103,7 +104,11 @@ const setupNotifications = () => {
|
|||||||
PushNotifications.requestPermissions().then(result => {
|
PushNotifications.requestPermissions().then(result => {
|
||||||
if (result.receive === 'granted') {
|
if (result.receive === 'granted') {
|
||||||
// Register with Apple / Google to receive push via APNS/FCM
|
// Register with Apple / Google to receive push via APNS/FCM
|
||||||
PushNotifications.register();
|
PushNotifications.register().then(() => {
|
||||||
|
FCM.subscribeTo({ topic: "all" })
|
||||||
|
.then((r) => alert(`Subscribed to topick all`))
|
||||||
|
.catch((err) => console.log(err));
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Show some error
|
// Show some error
|
||||||
}
|
}
|
||||||
@ -156,6 +161,20 @@ const registerFCM = () => {
|
|||||||
getToken(messaging.value, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then((currentToken) => {
|
getToken(messaging.value, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then((currentToken) => {
|
||||||
if (currentToken) {
|
if (currentToken) {
|
||||||
console.log(currentToken)
|
console.log(currentToken)
|
||||||
|
fetch('https://api.xeovalyte.com/subscribetotopic', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
|
||||||
|
'content-type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ topic: 'all', registrationToken: currentToken })
|
||||||
|
}).then(response => response.json())
|
||||||
|
.then(response => {
|
||||||
|
console.log(response)
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.log(err)
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Show permission request UI
|
// Show permission request UI
|
||||||
console.log('No registration token available. Request permission to generate one.');
|
console.log('No registration token available. Request permission to generate one.');
|
||||||
|
Loading…
Reference in New Issue
Block a user