使用超时创建Web通知

问题描述 投票:9回答:1

如何使用设置超时创建TypeScript Web通知?

我创建了以下代码,但在默认超时之前关闭通知不起作用:

export function createNotification(title: string, body: string) {
    let now: Date = new Date(Date.now());
    let date = now.add(5).seconds();

    let options = {
        body: body,
        requireInteraction: false,
        timestamp: date
    };
    new Notification(title, options);
}

作为测试,我想在TypeScript中创建一个持续五秒钟的通知,覆盖浏览器的默认行为。我看不到TypeScript打字文件中列出的任何其他选项对我有帮助:

ES2017 Notification Options

我正在使用Chrome开发和测试这个软件,虽然我使用Firefox作为我的主浏览器。因此,任何在Chrome中正常运行但不包含黑客或浏览器特定代码的解决方案都可以。

typescript datejs web-notifications
1个回答
7
投票

你尝试过Notification.close()吗?

function spawnNotification(theBody,theIcon,theTitle) {
  var options = {
      body: theBody,
      icon: theIcon
  }

  var n = new Notification(theTitle,options);
  setTimeout(n.close.bind(n), 4000);
}

来源:https://developer.mozilla.org/en-US/docs/Web/API/Notification/close

© www.soinside.com 2019 - 2024. All rights reserved.