Add title and image url in notification
This commit is contained in:
@ -4,10 +4,8 @@ import { UserDeviceToken } from '../entity/user_device_tokens'
|
||||
import { env } from '../env'
|
||||
import { ContentReader } from '../generated/graphql'
|
||||
import { analytics } from '../utils/analytics'
|
||||
import { sendMulticastPushNotifications } from '../utils/sendNotification'
|
||||
import { getNewsletterEmail } from './newsletters'
|
||||
import { SaveContext, saveEmail, SaveEmailInput } from './save_email'
|
||||
import { getDeviceTokensByUserId } from './user_device_tokens'
|
||||
import { Page } from '../elastic/types'
|
||||
import { addLabelToPage } from './labels'
|
||||
import { saveSubscription } from './subscriptions'
|
||||
@ -97,19 +95,19 @@ export const saveNewsletterEmail = async (
|
||||
})
|
||||
console.log('newsletter label added:', result)
|
||||
|
||||
// sends push notification
|
||||
const deviceTokens = await getDeviceTokensByUserId(newsletterEmail.user.id)
|
||||
if (!deviceTokens) {
|
||||
console.log('Device tokens not set:', newsletterEmail.user.id)
|
||||
return true
|
||||
}
|
||||
|
||||
const multicastMessage = messageForLink(page, deviceTokens)
|
||||
await sendMulticastPushNotifications(
|
||||
newsletterEmail.user.id,
|
||||
multicastMessage,
|
||||
'newsletter'
|
||||
)
|
||||
// // sends push notification
|
||||
// const deviceTokens = await getDeviceTokensByUserId(newsletterEmail.user.id)
|
||||
// if (!deviceTokens) {
|
||||
// console.log('Device tokens not set:', newsletterEmail.user.id)
|
||||
// return true
|
||||
// }
|
||||
//
|
||||
// const multicastMessage = messageForLink(page, deviceTokens)
|
||||
// await sendMulticastPushNotifications(
|
||||
// newsletterEmail.user.id,
|
||||
// multicastMessage,
|
||||
// 'newsletter'
|
||||
// )
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@ -19,9 +19,10 @@ interface PubSubRequestBody {
|
||||
}
|
||||
|
||||
export interface PubSubData {
|
||||
subscription: string
|
||||
userId: string
|
||||
type: EntityType
|
||||
subscription?: string
|
||||
image?: string
|
||||
}
|
||||
|
||||
enum EntityType {
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user