2016-09-13 7 views
0

이 스 니펫은 포커스가있는 앱 탭을 가져 오거나 해당 URL로 새 탭을 여는 데 도움이됩니다. 탭이 이미 열려있는 경우 사용자가 클릭 한 알림을 기반으로 URL을 변경할 수있는 방법이 있습니까?서비스 담당자 : 알림을 클릭 할 때 다른 URL로 사용자 리디렉션

event.waitUntil(
    clients.matchAll({ 
     type: 'window' 
    }) 
    .then(function(windowClients) { 
     for (var i = 0; i < windowClients.length; i++) { 
      var client = windowClients[i]; 
      if (url.test(client.url) && 'focus' in client) { 
       return client.focus(); 
      } 
     } 
     if (clients.openWindow) { 
      return clients.openWindow('/#/chat'); 
     } 
    }) 
); 
+1

다른 아키텍처를 제안하면 클라이언트에게 메시지를 보내어 필요한 상황에서 수행 할 작업을 결정할 수 있습니다. IOW, 포커스가 필요한 경우 'client.postMessage ('focus ')'. 새 창을 열어야하는 경우 'client.postMessage ('window ', url)'. 리디렉션해야 할 경우 'client.postMessage ('redirect ', url)' –

+0

동의! 이것은 내가 지금하고있는 일이지만, 나는 이미 태그를 가지고 있기 때문에 그것을 여기에서 할 수 있었는지 궁금해하고 있었다. – Vibgy

답변

4

WindowClient.navigate() 메서드는 원하는대로 들립니다. 다음과 같이 할 수 있어야합니다 :

// Assume that you want to find the first window that is currently open 
// to originalUrl, and navigate that window to navigationUrl. 
// If there is no window currently at originalUrl, then open a new window. 
event.waitUntil(
    clients.matchAll({type: 'window'}) 
    .then(clients => clients.filter(client => client.url === originalUrl)) 
    .then(matchingClients => { 
     if (matchingClients[0]) { 
     return matchingClients[0].navigate(navigationUrl) 
       .then(client => client.focus()); 
     } 

     return clients.openWindow(navigationUrl); 
    }) 
);