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

Asked

Viewed 374 times

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.

inserir a descrição da imagem aqui

What I want is at least when the user clicks on the notification go to the tab where the chat is without giving the refresh of the page, the excerpt I did was like this, but it updates the page and does not go to the "chat tab":

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

The complete code of the notification function:

    //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');
              };
        }

2 answers

2


1

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

This brings the focus to the window that generated the notification.

I mean, do

notification.onclick = function () {
     window.focus();
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.