Skip to content

Instantly share code, notes, and snippets.

@reberthkss
Last active July 30, 2021 15:29
Show Gist options
  • Save reberthkss/1696ba1ef9da1f067a86de5d7cf3638b to your computer and use it in GitHub Desktop.
Save reberthkss/1696ba1ef9da1f067a86de5d7cf3638b to your computer and use it in GitHub Desktop.
import * as functions from "firebase-functions";
import * as admin from "firebase-admin"
const express = require('express');
const app = express();
const firebaseApp = admin.initializeApp();
// @ts-ignore
app.get("/:notificationType/:summonerName", async (req, res) => {
try {
const summonerName: string = req.params.summonerName;
const notificationType: string = req.params.notificationType;
const sanitizedName: string = summonerName.toLowerCase().split(" ").join("");
let notification = {
title: "",
message: ""
};
const tokens = (await firebaseApp
.firestore()
.collection("players")
.doc(sanitizedName)
.get()).data();
switch (notificationType) {
case "matchFound": {
notification.title = "Match has been found!";
notification.message = "10 Seconds to accept... (this is not a counter!)";
break;
}
case "gameStarted": {
notification.title = "The match started!!!";
notification.message = `Hey ${summonerName}, the match started!!!`;
break;
}
}
if (tokens) {
firebaseApp
.messaging()
.sendMulticast({
tokens: tokens["token"],
android: {
priority: 'high',
notification: {
title: notification.title,
body: notification.message,
priority: 'high',
defaultLightSettings: true,
defaultSound: true,
defaultVibrateTimings: true,
channelId: '500'
}
},
})
.then((res) => console.log("Success!!", res))
.catch((e) => console.log("Error!!"));
}
res.send("Ok!");
} catch (e) {
console.log("Error => ", e.message);
res.send(e.message);
}
})
export const sendNotification = functions.https.onRequest(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment