Skip to content

Commit

Permalink
refacor: remove the unnecessary module and added validations in notif…
Browse files Browse the repository at this point in the history
…ication service

Signed-off-by: KulkarniShashank <[email protected]>
  • Loading branch information
KulkarniShashank committed Feb 6, 2024
1 parent ecf3c15 commit 6e15546
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@aries-framework/core": "0.4.2",
"@aries-framework/node": "0.4.2",
"@hyperledger/aries-askar-nodejs": "^0.1.1",
"axios": "^1.6.5",
"express": "^4.18.1",
"firebase-admin": "^11.10.1",
"prettier": "^2.8.4",
Expand Down
13 changes: 2 additions & 11 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import {
AGENT_ENDPOINTS,
AGENT_NAME,
AGENT_PORT,
FIREBASE_CLIENT_EMAIL,
FIREBASE_PRIVATE_KEY,
FIREBASE_PROJECT_ID,
LOG_LEVEL,
NOTIFICATION_WEBHOOK_URL,
POSTGRES_HOST,
USE_PUSH_NOTIFICATIONS,
WALLET_KEY,
Expand Down Expand Up @@ -141,14 +139,7 @@ export async function createAgent() {
await agent.initialize()

// Register all event handlers and initialize fcm module
if (USE_PUSH_NOTIFICATIONS) {
// initializeApp({
// credential: credential.cert({
// projectId: FIREBASE_PROJECT_ID,
// clientEmail: FIREBASE_CLIENT_EMAIL,
// privateKey: FIREBASE_PRIVATE_KEY,
// }),
// })
if (USE_PUSH_NOTIFICATIONS && NOTIFICATION_WEBHOOK_URL) {
routingEvents(agent)
}

Expand Down
86 changes: 41 additions & 45 deletions src/push-notifications/fcm/services/PushNotificationsFcmService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,11 @@ import {
import { PushNotificationsFcmProblemReportError, PushNotificationsFcmProblemReportReason } from '../errors'
import { PushNotificationsFcmSetDeviceInfoMessage, PushNotificationsFcmDeviceInfoMessage } from '../messages'
import { PushNotificationsFcmRecord, PushNotificationsFcmRepository } from '../repository'

import * as admin from 'firebase-admin'
import { FIREBASE_NOTIFICATION_TITLE, NOTIFICATION_WEBHOOK_URL } from '../../../constants'
import axios from 'axios';

interface NotificationBody {
title?: string;
body?: string;
}

interface ApsPayload {
aps: {
sound: string;
};
}

interface Apns {
payload: ApsPayload;
}
import { NOTIFICATION_WEBHOOK_URL } from '../../../constants'
import fetch from 'node-fetch'

interface NotificationMessage {
notification: NotificationBody;
apns: Apns;
messageType: string;
token: string;
clientCode: string;
}
Expand Down Expand Up @@ -118,7 +100,7 @@ export class PushNotificationsFcmService {
try {
// Get the session for the connection
// const session = await this.transportService.findSessionByConnectionId(connectionId)

// if (session) {
// this.logger.info(`Connection ${connectionId} is active. So skip sending notification`)
// return
Expand All @@ -129,25 +111,15 @@ export class PushNotificationsFcmService {
connectionId,
})

if (!pushNotificationFcmRecord?.deviceToken) {
this.logger.info(`No device token found for connectionId so skip sending notification`)
return
}
// 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: NotificationMessage = {
notification: {
title: FIREBASE_NOTIFICATION_TITLE,
body: messageType,
},
apns: {
payload: {
aps: {
sound: 'default',
},
},
},
messageType,
token: pushNotificationFcmRecord?.deviceToken || '',
clientCode: pushNotificationFcmRecord?.clientCode || ''
}
Expand All @@ -169,20 +141,44 @@ export class PushNotificationsFcmService {

public async processNotification(message: NotificationMessage) {
try {
if (NOTIFICATION_WEBHOOK_URL) {
const payload = {
fcmToken: message.token || '',
'@type': message.notification.body,
clientCode: message.clientCode || '5b4d6bc6-362e-4f53-bdad-ee2742bc0de3'
}
await axios.post(NOTIFICATION_WEBHOOK_URL, payload);
} else {
if (!NOTIFICATION_WEBHOOK_URL) {
this.logger.error("Notification webhook URL not found");
return
}

const body = {
fcmToken: message.token || 'abc',
messageType: message.messageType,
clientCode: message.clientCode || '5b4d6bc6-362e-4f53-bdad-ee2742bc0de3'
}
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
};

fetch(NOTIFICATION_WEBHOOK_URL, requestOptions)
.then(response => {
if (!response.ok) {
this.logger.error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
this.logger.debug(`Data: ${data}`);
})
.catch(error => {
this.logger.error(`Error: ${error}`);
});

} catch (error) {
this.logger.error(`Error sending notification`, {
cause: error,
})
}
}
}


0 comments on commit 6e15546

Please sign in to comment.