I wanted to send notifications to specific users from NodeJs. The idea is to be independant from firebase and any kind of third party server if it's possible. I'm finding a lot of info about how to build a service worker and a lot of info about show notifications, but I can't find anything that could work for me about sendind the push messages to the workker to be shown.
Here is the service-worker.js
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/layout/service-worker.js')
.then(function (registration) {
registration.pushManager.subscribe({
userVisibleOnly: true
}).then(function (subscription) {
isPushEnabled = true;
console.log("subscription.endpoint: ", subscription.endpoint);
//Here I will have to store the endpoint on my DB
});
}).catch(function (err) {
console.log(err);
});
}
//I think that this could be the listener, but I don't know how to trigger it from the server
this.onpush = function (event) {
console.log(event.data);
}
I have this code for show the notification (when I could send notifications)
var notificationTitle = 'Title';
var notificationOptions = {
"body": "Some text",
//"icon": "images/icon.png",
"vibrate": [200, 100, 200, 100, 200, 100, 400]
}
showNotification(notificationTitle, notificationOptions);
The idea is to implement this code inside the "onpush" event qhen I will know what format I will recieve.
So I need a method about send the push to those methods, but don't now how to. I read something about node web-push module with VAPID keys, but I still not found the sender jet. I have a manifest.json that I don't know if it really do anything (I read that is necesary for Chrome navigator, but no idea, I'm testing all in Chrome, Firefox and Edge).
Aditionally I'm using http in localhost to test it. I don't really know if this will work without an SSL certificate or an autosigned one.
Every help would be apreciated.