Add title and image url in notification

This commit is contained in:
Hongbo Wu
2022-11-22 10:50:38 +08:00
parent 2cb22097ca
commit 9344fae1b9
4 changed files with 35 additions and 20 deletions

View File

@ -19,9 +19,10 @@ interface PubSubRequestBody {
}
export interface PubSubData {
subscription: string
userId: string
type: EntityType
subscription?: string
image?: string
}
enum EntityType {

View File

@ -94,7 +94,14 @@ export const triggerActions = async (
console.log('No notification messages provided')
continue
}
await sendNotification(userId, action.params, apiEndpoint, jwtSecret)
await sendNotification(
userId,
data.subscription,
action.params,
apiEndpoint,
jwtSecret,
data.image
)
}
}
}
@ -102,16 +109,21 @@ export const triggerActions = async (
export const sendNotification = async (
userId: string,
subscription: string,
messages: string[],
apiEndpoint: string,
jwtSecret: string
jwtSecret: string,
image?: string
) => {
const title = `📫 - ${subscription} has published a new article`
// get device tokens by calling api
const tokens = await getDeviceTokens(userId, apiEndpoint, jwtSecret)
const batchMessages = getBatchMessages(
title,
messages,
tokens.map((t) => t.token)
tokens.map((t) => t.token),
image
)
return sendBatchPushNotifications(batchMessages)

View File

@ -56,8 +56,10 @@ export const getDeviceTokens = async (
}
export const getBatchMessages = (
title: string,
messages: string[],
tokens: string[]
tokens: string[],
imageUrl?: string
): Message[] => {
const batchMessages: Message[] = []
messages.forEach((message) => {
@ -65,7 +67,9 @@ export const getBatchMessages = (
batchMessages.push({
token,
notification: {
title,
body: message,
imageUrl,
},
})
})