Closed ghost closed 5 years ago
I have been trying to fix this error for the past 4 day. Only source I got was https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user .Hope this helps you as it is a completely new thing for me. Please help!!
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const webpush = require('web-push')
const app = express()
app.use(cors())
app.use(bodyParser.json())
const port = 4000
app.get('/' , (req,res) => res.send('Hello World!'))
// const dummyDb = { subscription:null }
const dummyDb = { subscription: "test-subscription" }
const saveToDatabase = async subscription => {
dummyDb.subscription = subscription
//Stored in dummy memory
}
app.post('/save-subscription', async (req,res) =>{
try
{
const subscription = req.body
await saveToDatabase(subscription)
res.json({message: 'success' })
}
catch(err)
{
console.log("New Error 1",err)
}
})
///To save the subscription from the user
const vapidKeys = {
publicKey: 'BG8p96lD4mhy2yCJctyh_8jMty65fNZxTY8aSQ8RE73nIAhy1X2iygTjzMemif8AkRWGgXHrGahDyuLfwyeZkHk',
privateKey: 'j08wy91EFkyLFwfvXc6jf8Kgk_PiEyvb9K7DdHoGCE0'
}
///To send the push notifications
webpush.setVapidDetails(
'mailto:harshit@clevertap.com',
vapidKeys.publicKey,
vapidKeys.privateKey
)
const sendNotification1 = (subscription, dataToSend='someRandomData') => {
try
{
webpush.sendNotification(subscription, dataToSend)
console.log("Sent finally")
}
catch(err)
{
console.log("New Error 2",err)
}
}
//route to test send notification
app.get('/send-notification',(req, res) => {
try
{
const subscription = dummyDb.subscription
const message = 'Hello World1'
sendNotification1(subscription,message)
res.json({message: 'message sent'})
}
catch(err)
{
console.log("New Error 3",err)
}
})
app.listen(port, () => console.log('Example app listening on port ${port}!'))
This is my Backend file index.js
What is the error you are getting @soorabali ?
(node:1386) UnhandledPromiseRejectionWarning: Error: You must pass in a subscription with at least an endpoint.
Log your subscription and see if it looks like this:
{
"endpoint":"https://updates.push.services.mozilla.com:443/wpush/v1/<some_id>",
"keys":{
"auth":"<some key>",
"p256dh":"<some key>"
}
}
Yeah it looks similar I'm attaching the console log that I have stored in my notepad
On Tue, May 21, 2019 at 6:01 PM Atul R notifications@github.com wrote:
Log your subscription and see if it looks like this:
{ "endpoint":"https://updates.push.services.mozilla.com:443/wpush/v1/
", "keys":{ "auth":" ", "p256dh":" " } } — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/master-atul/web-push-demo/issues/1?email_source=notifications&email_token=AJ3NSQ5ADGNQ6NWXEE4RLYLPWPTT3A5CNFSM4HOKIGE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV3YDAQ#issuecomment-494371202, or mute the thread https://github.com/notifications/unsubscribe-auth/AJ3NSQ3KBPGWFDBH2RJXEUTPWPTT3ANCNFSM4HOKIGEQ .
-- Harshit Soora Sophomore Department of Computer Science and Engineering IIT Kharagpur
What to do next man? I'm still stuck in the situation.
service worker activate { "endpoint": "https://updates.push.services.mozilla.com/wpush/v2/gAAAAABc5Qreaadp1ntrH0QqSNstSc3EP1orhE4gUVci4q4sDD39IDJqnbTVivE_BILRIvbDg0Pc7fhYV5OzAg4qg2XlAGS0j44tUJpvh31QJAeOlHBz8w5eRdvIXTON2xLWnDFN5N-X5IvRv4vhsuWnjOZWyfB-sJ_8C5jQrFDJd8ItN6gj0vw", "keys": { "auth":"IaVzpQvVFF7tBxRBuu5OTw", "p256dh":"BFFLAwD1SUIzzv8l0HROHaTNCQEEa6npCb8q4o2ZmEDNu3WhRQrXwTbySAXehfTa7eUY MVarlLNcLus6Vwzwo3s" } } Object { message: "success" }
Error is been solved....was just a typo
Hey, can you please suggest how did you resolve this error? I am stuck on the same for a week. Testing it on Google chrome.
Thank you.
Can someone explain how you get the subscription?
Handle zero/null subscribers case. And this should be good then.
//function to send the notification to the subscribed device
const sendNotification = (subscription, dataToSend) => {
if (subscription === null) {
console.log("No subscribers in the sys yet.");
} else {
webpush.sendNotification(subscription, dataToSend);
}
}
Error: You must pass in a subscription with at least an endpoint.
I encountered the same error, doing a hard reload (CTRL + SHIFT + R)
of my web page solved my problem.
There has been an error in the console as mentioned in the title. The notification is not pushed to the frontend. The endpoint as mentioned in your blog https://medium.com/izettle-engineering/beginners-guide-to-web-push-notifications-using-service-workers-cb3474a17679 is not working (ie. localhost:4000/save-subscription)