Patrick
Patrick

Reputation: 2577

How do I make a Notification stay open until the user closes it?

Is there a setting that will allow me to keep a Notification open until a user clicks it?

                      if (("Notification" in window)) {

                          Notification.requestPermission(function() {
                                var notification = 
                                    new Notification('Hello', 
                                        { 
                                            body : 'Hello',
                                            icon: 'https://www.domain.com/images/live_chat_icon.png',
                                            tag: 'Test' ,
                                        });

                                    notification.onclick    = function(event) {
                                      event.preventDefault(); // prevent the browser from focusing the Notification's tab
                                      window.open('https://www.domain.com', '_blank');
                                    }
                                            window.navigator.vibrate(500);
                            });
                      }

Upvotes: 1

Views: 2425

Answers (1)

Wouter Klein Heerenbrink
Wouter Klein Heerenbrink

Reputation: 1391

According to the docs there is a boolean requireInteraction:

A Boolean indicating that on devices with sufficiently large screens, a notification should remain active until the user clicks or dismisses it. https://developer.mozilla.org/en-US/docs/Web/API/notification

new Notification('Hello', { 
   body : 'Hello', 
   requireInteraction: true 
});

Tested in Chrome on MacOS.

Upvotes: 8

Related Questions