1

我想顯示我的通知給用戶,直到我希望默認情況下它顯示約19秒約。有人可以告訴我關於這方面的任何詭計嗎?如何增加'chrome推送通知'的可見度時間?

我也嘗試一次又一次地更新,以保持顯示但不成功,實際上沒有得到適當的語法來做到這一點。 目前我正在使用以下代碼註冊服務人員。 通過此代碼我可以顯示約19秒的通知,但我想顯示它1分鐘。

var url = "https://example.com/json-data.php?param="+Math.random(); 
self.addEventListener('push', function(event) {  
    event.waitUntil( 
    fetch(url).then(function(response) { 
     if (response.status !== 200) { 
     // Either show a message to the user explaining the error 
     // or enter a generic message and handle the 
     // onnotificationclick event to direct the user to a web page 
     console.log('Looks like there was a problem. Status Code: ' + response.status); 
     throw new Error(); 
     } 

     // Examine the text in the response 
     return response.json().then(function(data) { 
     if (data.error || !data.notification) { 
      console.log('The API returned an error.', data.error); 
      throw new Error(); 
     } 
     var title = data.notification.title; 
     var message = data.notification.message; 
     var icon = data.notification.icon; 

     return self.registration.showNotification(title, { 
      body: message, 
      icon: icon, 
      data: { 
      url: data.notification.url 
      } 
     }); 
     }); 
    }).catch(function(err) { 
     console.log('Unable to retrieve data', err); 

     var title = 'An error occurred'; 
     var message = 'We were unable to get the information for this push message'; 
     var icon = 'img/design19.jpg'; 
     var notificationTag = 'notification-error'; 
     return self.registration.showNotification(title, { 
      body: message, 
      icon: icon, 
      tag: notificationTag 
     }); 
    }) 
); 
}); 

// The user has clicked on the notification ... 
self.addEventListener('notificationclick', function(event) { 
    console.log(event.notification.data.url); 
    // Android doesn't close the notification when you click on it 
    // See: http://crbug.com/463146 
    event.notification.close(); 

    // 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 == '/' && 'focus' in client) 
      return client.focus(); 
     } 
     if (clients.openWindow) { 
     return clients.openWind`enter code here`ow(event.notification.data.url); 
     }`enter code here` 
    }) 
); 
}); 

回答

0

我認爲你不能直接設置通知應該顯示多長時間。

一種可能哈克的方式做到這一點是,一旦瀏覽器將支持persistent通知(我不知道,如果Chrome或Firefox做的那一刻),以示永久通知,然後在超時後關閉它。

0

目前沒有設置通知超時的參數。默認情況下,通知將顯示20秒,然後桌面版Chrome會自動將通知最小化。

或者,在選項requireInteraction中有一個參數,默認爲false。通過啓用true將使通知在用戶與之交互之前保持可見狀態。