mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fix send notification failure
This commit is contained in:
parent
ecc217deef
commit
075c1b4d14
4 changed files with 46 additions and 29 deletions
|
|
@ -77,8 +77,9 @@ export const getPubSubSubscriptionOptions = async (
|
|||
}
|
||||
|
||||
options.pushConfig = {
|
||||
pushEndpoint: `${env.queue.notificationEndpoint}/${userId}?token=${env.queue.verificationToken}`,
|
||||
pushEndpoint: `${env.queue.notificationEndpoint}?token=${env.queue.verificationToken}`,
|
||||
attributes: {
|
||||
userId,
|
||||
filter,
|
||||
messages: JSON.stringify(params),
|
||||
tokens: JSON.stringify(deviceTokens.map((t) => t.token)),
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
"dependencies": {
|
||||
"@google-cloud/functions-framework": "3.1.2",
|
||||
"@google-cloud/pubsub": "^3.2.1",
|
||||
"dotenv": "^16.0.1",
|
||||
"firebase-admin": "^10.0.2",
|
||||
"@sentry/serverless": "^6.16.1"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,17 @@ import * as Sentry from '@sentry/serverless'
|
|||
import { Request, Response } from 'express'
|
||||
import { sendBatchPushNotifications } from './sendNotification'
|
||||
import { Message } from 'firebase-admin/lib/messaging'
|
||||
import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
|
||||
|
||||
dotenv.config()
|
||||
|
||||
interface SubscriptionAttributes {
|
||||
messages: string[]
|
||||
tokens: string[]
|
||||
}
|
||||
|
||||
interface SubscriptionData {
|
||||
attributes?: string
|
||||
attributes?: SubscriptionAttributes
|
||||
data: string
|
||||
}
|
||||
|
||||
|
|
@ -22,7 +30,10 @@ const readPushSubscription = (req: Request): SubscriptionData | null => {
|
|||
return null
|
||||
}
|
||||
|
||||
const body = req.body as { message: { data: string }; attributes?: string }
|
||||
const body = req.body as {
|
||||
message: { data: string }
|
||||
attributes?: SubscriptionAttributes
|
||||
}
|
||||
const data = Buffer.from(body.message.data, 'base64').toString('utf-8')
|
||||
|
||||
return {
|
||||
|
|
@ -49,30 +60,32 @@ const getBatchMessages = (messages: string[], tokens: string[]): Message[] => {
|
|||
|
||||
export const notification = Sentry.GCPFunction.wrapHttpFunction(
|
||||
async (req: Request, res: Response) => {
|
||||
const subscriptionData = readPushSubscription(req)
|
||||
if (!subscriptionData) {
|
||||
res.status(400).send('Invalid request')
|
||||
return
|
||||
}
|
||||
|
||||
const { attributes } = subscriptionData
|
||||
if (!attributes) {
|
||||
res.status(400).send('Invalid request')
|
||||
return
|
||||
}
|
||||
|
||||
const { messages, tokens } = JSON.parse(attributes) as {
|
||||
messages: string[]
|
||||
tokens: string[]
|
||||
}
|
||||
if (!messages || messages.length === 0 || !tokens || tokens.length === 0) {
|
||||
res.status(400).send('Invalid request')
|
||||
return
|
||||
}
|
||||
|
||||
const batchMessages = getBatchMessages(messages, tokens)
|
||||
|
||||
try {
|
||||
const subscriptionData = readPushSubscription(req)
|
||||
if (!subscriptionData) {
|
||||
res.status(400).send('Invalid request')
|
||||
return
|
||||
}
|
||||
|
||||
const { attributes } = subscriptionData
|
||||
if (!attributes) {
|
||||
res.status(400).send('Invalid request')
|
||||
return
|
||||
}
|
||||
|
||||
const { messages, tokens } = attributes
|
||||
if (
|
||||
!messages ||
|
||||
messages.length === 0 ||
|
||||
!tokens ||
|
||||
tokens.length === 0
|
||||
) {
|
||||
res.status(400).send('Invalid request')
|
||||
return
|
||||
}
|
||||
|
||||
const batchMessages = getBatchMessages(messages, tokens)
|
||||
|
||||
await sendBatchPushNotifications(batchMessages)
|
||||
|
||||
res.status(200).send('OK')
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { initializeApp } from 'firebase-admin/app'
|
||||
import { applicationDefault, initializeApp } from 'firebase-admin/app'
|
||||
import {
|
||||
BatchResponse,
|
||||
getMessaging,
|
||||
|
|
@ -7,7 +7,9 @@ import {
|
|||
} from 'firebase-admin/messaging'
|
||||
|
||||
// getting credentials from App Engine
|
||||
initializeApp()
|
||||
initializeApp({
|
||||
credential: applicationDefault(),
|
||||
})
|
||||
|
||||
export const sendPushNotification = async (
|
||||
message: Message
|
||||
|
|
@ -25,7 +27,7 @@ export const sendBatchPushNotifications = async (
|
|||
messages: Message[]
|
||||
): Promise<BatchResponse | undefined> => {
|
||||
const res = await getMessaging().sendAll(messages)
|
||||
console.debug('success count: ', res.successCount)
|
||||
console.debug('res', res)
|
||||
|
||||
return res
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue