20 lines
585 B
JavaScript
20 lines
585 B
JavaScript
|
import { getMessaging } from 'firebase-admin/messaging'
|
||
|
|
||
|
export default defineEventHandler(async event => {
|
||
|
const { topic, registrationToken } = await readBody(event);
|
||
|
|
||
|
|
||
|
if (!topic) throw createError({ statusCode: 400, statusMessage: 'no-topic'})
|
||
|
if (!registrationToken) throw createError({ statusCode: 400, statusMessage: 'no-registrationtoken'})
|
||
|
|
||
|
try {
|
||
|
await getMessaging().subscribeToTopic([registrationToken], topic)
|
||
|
|
||
|
return { code: 'success'}
|
||
|
} catch (e) {
|
||
|
console.log(e)
|
||
|
|
||
|
throw createError({ statusCode: 500, statusMessage: e.message })
|
||
|
}
|
||
|
})
|