Click on Notification Desktop and go to the same window without refreshing the page

5

I am developing a chat with WebSocket and I have a problem, when I send a message to a friend, he receives the desktop notification (from the Browser he is) and when he clicks on the notification he goes to the conversation window of the person who sent the message to him.

WhatIwantisatleastwhentheuserclicksonthenotificationgotothetabwherethechatiswithoutgivingtherefreshofthepage,theexcerptImadewasso,butitrefreshesthepageanddoesnotgotothe" of the chat ":

notification.onclick = function () {
     window.open('http://localhost/pullchat', '_self');
};

The complete Notification function code:

    //Função de notificação de mensagens
        function notifyMe(nome, mensagem, id) {
              if (!Notification) {
                alert('Notifications are supported in modern versions of Chrome, Firefox, Opera and Firefox.');
                return;
              }

              if (Notification.permission !== "granted")
                Notification.requestPermission();

              var notification = new Notification( nome, {
                icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
                body: mensagem,
              });

//------------------------AQUI QUE ESTÁ O PROBLEMA-------------------------------
              notification.onclick = function () {
                window.open('http://localhost/pullchat', '_self');
              };
        }
    
asked by anonymous 10.05.2015 / 17:56

2 answers

2

You can use window.focus() , however this may not work for all browsers, as the security settings for each may vary according to the user's privacy policies.

There are two topics in stackOverflow English that can help you:

12.02.2016 / 20:30
1

According to this question in SOEn , just use window.focus() where you are using window.open .

This takes focus to the window that generated the notification.

That is, do

notification.onclick = function () {
     window.focus();
};
    
12.02.2016 / 20:30