Last active
November 27, 2023 11:27
-
-
Save divyasonaraa/923ef3ef9e06201ec66b7507c060d25c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let webpush = require("web-push"); | |
const publicKey = | |
"Your-public-key"; | |
const privateKey = "Your-private-key"; | |
/* | |
config - webpush | |
*/ | |
webpush.setVapidDetails( | |
"mailto:youremail@gmail.com", | |
publicKey, | |
privateKey | |
); | |
/* | |
endpoint- create post | |
*/ | |
app.post("/blog", (request, response) => { | |
response.set("Access-Control-Allow-Origin", "*"); | |
let uuid = UUID(); | |
let fields = {}; | |
let fileData = {}; | |
const bb = busboy({ headers: request.headers }); | |
bb.on("file", (name, file, info) => { | |
const { filename, encoding, mimeType } = info; | |
let filepath = path.join(os.tmpdir(), filename); | |
file.pipe(fs.createWriteStream(filepath)); | |
fileData = { filepath, mimeType }; | |
}); | |
bb.on("field", (name, val, info) => { | |
console.log(`Field [${name}]: value: %j`, val); | |
fields[name] = val; | |
}); | |
bb.on("close", () => { | |
db.collection("blogs") | |
.doc(fields.id) | |
.set({ | |
id: fields.id, | |
title: fields.title, | |
content: fields.content, | |
created_at: parseInt(fields.created_at), | |
updated_at: parseInt(fields.created_at), | |
image_url: fields.image_url, | |
liked: fields.liked, | |
favorite: fields.favorite, | |
}) | |
.then(() => { | |
sendPushNotification(); | |
response.send("blog added: " + fields.id); | |
}); | |
function sendPushNotification() { | |
let subscriptions = []; | |
db.collection("subscriptions") | |
.get() | |
.then((snapshot) => { | |
snapshot.forEach((doc) => { | |
subscriptions.push(doc.data()); | |
}); | |
return subscriptions; | |
}) | |
.then((subscriptions) => { | |
subscriptions.forEach((subscription) => { | |
const pushSubscription = { | |
endpoint: subscription.endpoint, | |
keys: { | |
auth: subscription.keys.auth, | |
p256dh: subscription.keys.p256dh, | |
}, | |
}; | |
let pushContent = { | |
title: "New Blog Created!", | |
body: "New Blog Added! Check it out!", | |
openUrl: "/#/", | |
}; | |
let pushContentStringified = JSON.stringify(pushContent); | |
try { | |
webpush.sendNotification( | |
pushSubscription, | |
pushContentStringified | |
); | |
console.log("Notification Send"); | |
} catch (error) { | |
console.error("Error sending push notification:", error); | |
} | |
}); | |
}); | |
} | |
}); | |
request.pipe(bb); | |
}); | |
/* | |
endpoint - createSubscription | |
*/ | |
app.post("/createSubscription", (request, response) => { | |
response.set("Access-Control-Allow-Origin", "*"); | |
db.collection("subscriptions") | |
.add(request.query) | |
.then((docRef) => { | |
response.send({ | |
message: "Subscription added!", | |
postData: request.query, | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment