0

I have a code that sends a message to a user who is subscribed to notifications But if the tab with the site is closed, these notifications do not work, how can I make these notifications work even if the tab is closed?

notiSet(event) {

            function notifyMe() {
                function notifySend() {
                    let notification = new Notification (
                        "New post in site Name",
                        {
                            tag : "ache-mail",
                            body : "Lorem ipsum" + e,
                            icon : "http://localhost:3000/favicon.ico"
                        }
                    )
                }
                let e = 0
                setInterval( function () {
                    notifySend()
                    e++
                }, 1000)
            }
            if (process.client) {
                if (!("Notification" in window)) {
                    alert("Go to chrome or Mozilla")
                } else if (Notification.permission === "granted") {
                    setTimeout(notifyMe, 2000)
                } else if (Notification.permission !== "denied") {
                    Notification.requestPermission( function (permission) {
                        if (!("permission" in Notification)) {
                            Notification.permission = permission
                        }
                        if (permission === "granted") {
                            setTimeout(notifyMe, 2000)
                        }
                    })
                }
            }
        }

1 Answers1

0

You need to create serviceWorker to be able to send notification if tab is closed. See this doc.

and check if this answer help you.

Amit Kumar
  • 620
  • 4
  • 17