I'm trying to add notification in my website. I'm new to node.js and service workers. I'm using expressjs for the backend and a Mysql database.
I'm following this tutorial: Beginners guide to Web Push Notifications using Service Workers and everything worked well until I use a real database for the webpush subscription. Datas are correctly registered in my table but when I try to get those datas with SELECT for testing the send part, I got this error:
C:\...\web-push-lib.js:87
throw new Error('You must pass in a subscription with at least '
^
Error: You must pass in a subscription with at least an endpoint.
I know that the problem comes from the following part but I can't find out how to use the function to get the result of the query.
//route to test send notification
app.get('/send-notification', (req, res) => {
//connect to the pool
pool.getConnection(function (err, connection) {
if (err) throw err; // not connected!
const sub = { subscription: null };
connection.query(`SELECT webpushsub_info FROM webpushsub`, function (err, result, fields) {
// if any error while executing above query, throw error
if (err) throw err;
// if there is no error, you have the result
sub.subscription = result;
const message = 'Inscription done';
webpush.sendNotification(sub.subscription, message)
connection.release();
res.json({ message: 'message sent' });
});
});
})
When I log my result It returns this.
RowDataPacket {
webpushsub_info: '{ //that's the column name where I store subscriptions
"endpoint":"https://fcm.googleapis.com/fcm/send/some_endpoint",
"expirationTime":null,
"keys":{
"p256dh":"some key",
"auth":"an other key"
}
}',
...
}
I also tried to log only one element result[0].webpushsub_info
so I got exactly the same as the entry. "The expected format is the same output as JSON.stringify'ing a PushSubscription in the browser." - npm documentation for web-push .
After a good week-end of reflexion, I found the solution. Even if
The expected format is the same output as JSON.stringify'ing a PushSubscription in the browser. -npm documentation for web-push.
I had to JSON.parse the result to get an object/array and iterate on every results to send notification to all subscribed users.
app.get('/send-notification', (req, res) => {
//connect to the pool
pool.getConnection(function (err, connection) {
if (err) throw err; // not connected!
connection.query(`SELECT webpushsub_info FROM webpushsub`, function (err, results, fields) {
// if any error while executing above query, throw error
if (err) throw err;
// if there is no error, you have the result
results.forEach(result => {
var pushSubscription = JSON.parse(result.webpushsub_info);
var message = "Post envoyé";
console.log(pushSubscription);
webpush.sendNotification(pushSubscription, message).catch(err=>console.error(err));
});
connection.release();
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.