Skip to content

Commit

Permalink
update firebase notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Pedro da Silva committed Nov 6, 2023
1 parent 8b82c1d commit 1d00c1e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ path-for-js-ipfs-repo/

.env.*
postgres_data/
redis_data/
redis_data/

firebase.json
43 changes: 25 additions & 18 deletions packages/core/src/utils/pushNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import localesConfig from '../utils/locale.json';
import { AppUser } from '../interfaces/app/appUser';
import { Logger } from './logger';
import { Transaction } from 'sequelize';
import { utils } from '../../index';
import admin from 'firebase-admin';
import admin, { ServiceAccount } from 'firebase-admin';
import config from '../config';

export async function sendNotification(
Expand Down Expand Up @@ -83,15 +82,17 @@ export async function sendNotification(
await Promise.all(languages.map(fetchNotificationsFromPrismic));

// send notification by group of languages
Object.keys(prismicNotifications).forEach(async (key, i) => {
const prismicData = prismicNotifications[key];
sendFirebasePushNotification(
prismicData.users.map(el => el.walletPNT!),
prismicData.title,
prismicData.description,
params && (params instanceof Array ? params[i] : params)
).catch(error => utils.Logger.error('sendFirebasePushNotification' + error));
});
await Promise.all(
Object.keys(prismicNotifications).map(async (key, i) => {
const prismicData = prismicNotifications[key];
return sendFirebasePushNotification(
prismicData.users.map(el => el.walletPNT!),
prismicData.title,
prismicData.description,
params && (params instanceof Array ? params[i] : params)
);
})
);
} catch (error) {
Logger.error('Failed to add notification:', error);
}
Expand All @@ -116,11 +117,11 @@ export async function sendFirebasePushNotification(
tokens: tokens_batch
};

admin
const result = await admin
.messaging()
.sendEachForMulticast(message, process.env.NODE_ENV === 'developement')
.then(res => Logger.info(JSON.stringify(res)))
.catch(Logger.error);
.sendEachForMulticast(message, process.env.NODE_ENV === 'developement');

Logger.info(JSON.stringify(result));

if (i + batch > tokens.length) {
break;
Expand All @@ -133,9 +134,15 @@ export async function sendFirebasePushNotification(

export function initPushNotificationService() {
try {
// recover config file
const base64file = config.firebaseFileBase64;
const jsonConfig = JSON.parse(Buffer.from(base64file, 'base64').toString());
let jsonConfig: ServiceAccount = {};

try {
jsonConfig = require('./firebase.json');
} catch (error) {
// recover config file
const base64file = config.firebaseFileBase64;
jsonConfig = JSON.parse(Buffer.from(base64file, 'base64').toString());
}

admin.initializeApp({
credential: admin.credential.cert(jsonConfig)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"useUnknownInCatchVariables": false,
"skipLibCheck": true
},
"include": ["src", "index.ts"],
"include": ["src", "index.ts", "src/utils/firebase.json"],
"exclude": ["node_modules"]
}
4 changes: 3 additions & 1 deletion services/microcredit/src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export const _sendPushNotification = async (usersToNotify: number[], type: numbe
}
});

utils.pushNotification.sendNotification(
utils.Logger.info(`Sending ${users.length} notifications of type ${type}`);

await utils.pushNotification.sendNotification(
users,
type,
true,
Expand Down

0 comments on commit 1d00c1e

Please sign in to comment.