2016-07-05 31 views
2

我已經建立了一個web推送系統,它工作得很好。webpush,如果沒有標籤如何打開窗口

我遇到的問題是,在Mac上,如果用戶打開Firefox但沒有打開任何頁面,當他收到通知時,點擊將丟失。 我 噸根本不做任何事情。

這裏的相關部分

self.addEventListener('push', function(event) { 

    var jsonObj = event.data.json(); 
    var title = jsonObj.title; 

    event.waitUntil(
    self.registration.showNotification(title, { 
     'body': jsonObj.body, 
     'icon': jsonObj.icon, 
     'href': jsonObj.href, 
     'tag': jsonObj.tag 

    })); 

    self.addEventListener('notificationclick', function(event) { 
     event.notification.close(); 
     var href = jsonObj.href; 
     var tag = jsonObj.tag; 

     if (clients.openWindow) { 
     clients.openWindow(href); 
     } 


     /* 
     // This looks to see if the current is already open and 
     // focuses if it is 
     event.waitUntil(
     clients.matchAll({ 
      type: "window" 
     }) 
     .then(function(clientList) { 
      for (var i = 0; i < clientList.length; i++) { 
      var client = clientList[i]; 
      if (client.url == href && 'focus' in client) 
       return client.focus(); 
      } 
      if (clients.openWindow) { 
      return clients.openWindow(href); 
      } 
     }) 
    ); 
    */ 
    }); 

}); 

回答

3

我會建議你:

  1. 定義push事件處理程序外notificationclick事件處理程序,在全球範圍內。您可以通過data參數showNotification中的jsonObj,因此您可以在notificationclick處理程序的event對象中訪問它。你可以在這裏看到一個例子:https://github.com/mozilla/wp-web-push/blob/master/wp-web-push/lib/js/sw.php
  2. openWindow返回一個Promise,你應該調用event.waitUntil和openWindow返回的promise。
+0

謝謝,我會做一些測試併發布結果。 – sathia

+0

它似乎按預期工作,但它不能打開一個窗口,如果瀏覽器沒有:( – sathia

+2

目前Firefox中有一個錯誤,當沒有窗口打開(https:///bugzilla.mozilla.org/show_bug.cgi?id=1226434)。 – Marco

相關問題