Skip to content

Commit

Permalink
refactor: add send notification method to service
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Ranjit Tummalapalli <[email protected]>
  • Loading branch information
sairanjit committed Sep 22, 2023
1 parent 54d6285 commit 44eb696
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 62 deletions.
34 changes: 0 additions & 34 deletions src/events/PushNotificationEvent.ts

This file was deleted.

24 changes: 5 additions & 19 deletions src/events/RoutingEvents.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import type { ForwardMessageEvent } from '@aries-framework/core'

import { RecordDuplicateError, RoutingEventTypes } from '@aries-framework/core'
import { sendNotificationEvent } from './PushNotificationEvent'
import { RoutingEventTypes } from '@aries-framework/core'
import { MediatorAgent } from '../agent'

export const routingEvents = async (agent: MediatorAgent) => {
agent.events.on(RoutingEventTypes.ForwardMessageEvent, async (event: ForwardMessageEvent) => {
try {
const record = event.payload.connectionRecord

// Get device info from PushNotificationsFcmRecord
const fcmDeviceInfoRecord = await agent.modules.pushNotificationsFcm.getDeviceInfoByConnectionId(record.id)

if (fcmDeviceInfoRecord?.deviceToken) {
agent.config.logger.info(`Sending notification to ${fcmDeviceInfoRecord.connectionId}`)
// Send notification to device
await sendNotificationEvent(fcmDeviceInfoRecord, agent.config.logger)
} else {
agent.config.logger.info(`No device token found for connectionId so skipping send notification event`)
}
await agent.modules.pushNotificationsFcm.sendNotification(record.id)
} catch (error) {
if (error instanceof RecordDuplicateError) {
agent.config.logger.error(`Multiple device info found for connectionId ${event.payload.connectionRecord.id}`)
} else {
agent.config.logger.error(`Error sending notification`, {
cause: error,
})
}
agent.config.logger.error(`Error sending notification`, {
cause: error,
})
}
})
}
8 changes: 4 additions & 4 deletions src/push-notifications/fcm/PushNotificationsFcmApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ export class PushNotificationsFcmApi {
}

/**
* Get push notification record by `connectionId`
* Send push notification to device
*
* @param connectionId The connection ID string
* @returns Promise<PushNotificationsFcmRecord>
* @returns Promise<void>
*/
public async getDeviceInfoByConnectionId(connectionId: string) {
return this.pushNotificationsService.getDeviceInfo(this.agentContext, connectionId)
public async sendNotification(connectionId: string) {
return this.pushNotificationsService.sendNotification(this.agentContext, connectionId)
}
}
49 changes: 44 additions & 5 deletions src/push-notifications/fcm/services/PushNotificationsFcmService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { FcmDeviceInfo } from '../models/FcmDeviceInfo'
import type { AgentContext, InboundMessageContext, Logger } from '@aries-framework/core'

import { AriesFrameworkError, inject, InjectionSymbols, injectable } from '@aries-framework/core'
import { AriesFrameworkError, inject, InjectionSymbols, injectable, RecordDuplicateError } from '@aries-framework/core'

import { PushNotificationsFcmProblemReportError, PushNotificationsFcmProblemReportReason } from '../errors'
import { PushNotificationsFcmSetDeviceInfoMessage, PushNotificationsFcmDeviceInfoMessage } from '../messages'
import { PushNotificationsFcmRecord, PushNotificationsFcmRepository } from '../repository'

import * as admin from 'firebase-admin'
import { FIREBASE_NOTIFICATION_BODY, FIREBASE_NOTIFICATION_TITLE } from '../../../constants'

@injectable()
export class PushNotificationsFcmService {
private pushNotificationsFcmRepository: PushNotificationsFcmRepository
Expand Down Expand Up @@ -76,9 +79,45 @@ export class PushNotificationsFcmService {
}
}

public async getDeviceInfo(agentContext: AgentContext, connectionId: string) {
return this.pushNotificationsFcmRepository.findSingleByQuery(agentContext, {
connectionId,
})
public async sendNotification(agentContext: AgentContext, connectionId: string) {
try {
// Get the device token for the connection
const pushNotificationFcmRecord = await this.pushNotificationsFcmRepository.findSingleByQuery(agentContext, {
connectionId,
})

if (!pushNotificationFcmRecord?.deviceToken) {
this.logger.info(`No device token found for connectionId so skip sending notification`)
return
}

// Prepare a message to be sent to the device
const message = {
notification: {
title: FIREBASE_NOTIFICATION_TITLE,
body: FIREBASE_NOTIFICATION_BODY,
},
apns: {
payload: {
aps: {
sound: 'default',
},
},
},
token: pushNotificationFcmRecord.deviceToken,
}

this.logger.info(`Sending notification to ${pushNotificationFcmRecord?.connectionId}`)
await admin.messaging().send(message)
this.logger.info(`Notification sent successfully to ${pushNotificationFcmRecord.connectionId}`)
} catch (error) {
if (error instanceof RecordDuplicateError) {
this.logger.error(`Multiple device info found for connectionId ${connectionId}`)
} else {
this.logger.error(`Error sending notification`, {
cause: error,
})
}
}
}
}

0 comments on commit 44eb696

Please sign in to comment.