Add Click in Register Notification javascript

Viewed 14

I have a code that displays a notification to the user I would like to add a function so that the user clicks on the desired url from the notification. My api has data.slug this current url

How can I add onclick for each notification with different url?

let subscription_date = new Date().toISOString().split('T')[0]
let subscription_deals = [7]
let displayed_ids = []
let getRequest = 'http://localhost:3005/notify_updates?deals=' + subscription_deals.join() + '&date=' + subscription_date + '&exclude=' + displayed_ids.join()

self.addEventListener('install', event => {
    setTimeout( function () {

        setInterval( async function () {
            const response = await fetch(getRequest)
            const data = await response.json()

            console.log(displayed_ids)

            for (let i = 0; i < data.length; i++) {
                if (displayed_ids.includes(data[i].tour_id) != true) {

                    registration.showNotification(
                        data[i].title,
                        {
                            badge: 'http://localhost:3000/favicon.ico',
                            image: 'http://localhost:3000/favicon.ico',
                            body: data[i].post_teaser,
                            icon: 'http://localhost:3000/favicon.ico',
                            vibrate: [200, 100, 200, 100, 200, 100, 200],
                            // tag: 'vibration-sample' + i
                        }
                    )
                    displayed_ids.push(data[i].tour_id)
                    

                }
            }
        }, 1000)
    }, 2000)
})
0 Answers
Related