diff --git a/package.json b/package.json index d4bc73b..37929be 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/agent.ts b/src/agent.ts index 78c3e01..fa01ed8 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -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, @@ -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) } diff --git a/src/push-notifications/fcm/services/PushNotificationsFcmService.ts b/src/push-notifications/fcm/services/PushNotificationsFcmService.ts index 70813dd..0154538 100644 --- a/src/push-notifications/fcm/services/PushNotificationsFcmService.ts +++ b/src/push-notifications/fcm/services/PushNotificationsFcmService.ts @@ -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; } @@ -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 @@ -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 || '' } @@ -169,16 +141,38 @@ 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, @@ -186,3 +180,5 @@ export class PushNotificationsFcmService { } } } + +