Notification Service

 avatar
unknown
javascript
a year ago
1.1 kB
6
Indexable
const path = require("path");
const admin = require("firebase-admin");
const { getMessaging } = require("firebase-admin/messaging");
class Notification {
  constructor() {
    this.initialize();
  }
  async initialize() {
    console.log("=> Notification service started");
    admin.initializeApp({
      credential: admin.credential.cert(path.join(process.cwd(), "bingsportz-football-ad34f-25fd93928d22.json"))
    });
  }
  async sendNotification(title, body, imageUrl = "", topic = "com.bingsportz.football", sound = "default") {
    const messageBody = {
      apns: {
        payload: {
          aps: {
            sound
          }
        }
      },
      notification: {
        title,
        body,
        imageUrl
      },
      topic, // use here token if needed
    };
    if (!imageUrl) delete messageBody.notification.imageUrl;
    getMessaging()
      .send(messageBody)
      .then(() => console.log("=> Notification sent!"))
      .catch((_err) => console.log("=> Error Sending Notification", _err));
  }
}
const notification = new Notification();
module.exports = notification;
Editor is loading...
Leave a Comment