如何增加'chrome push notification'可见性时间?

时间:2016-02-29 08:40:34

标签: javascript google-chrome notifications push-notification service-worker

我希望向用户显示我的通知,直到我希望默认情况下显示最多19秒。有人可以告诉我有关这方面的任何伎俩吗?

我还试图一次又一次地更新以保持显示但不成功,实际上没有适当的语法来做到这一点。 目前我使用下面的代码注册服务工作者。 通过这个代码,我能够显示约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`
    })
  );
});

2 个答案:

答案 0 :(得分:1)

在设置通知超时时,目前没有参数。默认情况下,通知会显示20秒,然后桌面版Chrome会自动最小化通知。

或者,选项requireInteraction中有一个参数默认为false。启用此选项true会使通知保持可见,直到用户与其进行互动为止。

答案 1 :(得分:0)

我认为您无法直接设置通知的显示时间。

一种可能的hacky方式是,一旦浏览器支持persistent通知(我不知道Chrome或Firefox目前是否这样做),就会显示持续通知,然后关闭它超时后。

相关问题