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将使通知在用户与之交互之前保持可见状态。