Notification Service

mail@pastecode.io avatar
unknown
javascript
17 days ago
1.1 kB
3
Indexable
Never
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;
Leave a Comment