npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

notification-koro1

v1.1.1

Published

(浏览器桌面通知)Html5 Notification npm包

Downloads

382

Readme

notification-koro1

H5 notification:一个浏览器桌面通知 npm 包,求 Star

轻量:

体积不超过 2KB

安装:

npm i -S notification-koro1

使用:

插件在vue项目中使用的示例:.vue文件

1. 导入 && 初始化:

初始化需要两个参数:title(通知的标题)、options(配置),具体信息查阅wiki文章MDN

import notification from 'notification-koro1'; // 引入npm包
const notificationClass = new notification(title, options); // 初始化

2. 浏览器是否支持: support

notification-koro1初始化完毕之后,可以通过support字段来判断浏览器是否支持notificationAPI

if (notificationClass.support) {
  // 显示通知逻辑,以下所有步骤都要在这里调用
} else {
  // 浏览器不支持
}

3. 注册回调事件:

注册回调事件notificationEvent

notificationEvent接收一个对象参数,对象的每个属性值必须都是函数

下面是栗子:

// 点击弹窗的回调
const eventObj = {
  // 点击通知回调
  onclick: e => {
    console.log("点击通知打开百度", e);
    window.open("https://www.baidu.com/", "_blank");
  },
  // 通知显示回调
  onshow: e => {
    console.log("显示", e);
  },
  // 通知遇到错误回调
  onerror: e => {
    console.log("通知报错", e);
  },
  // 通知关闭回调
  onclose: e => {
    console.log("关闭通知", e);
  }
};
this.notificationClass.notificationEvent(eventObj);

注意:

  1. 注册回调事件需要在请求通知之前触发,否则事件无法绑定到通知上
  2. 有多个通知,想绑定不同的回调事件,再次调用这个API,绑定新的通知
  3. 插件会对对象参数和对象属性的value值进行检测,检测不通过的话,将不会绑定回调。

4. 请求用户授权

const userSelectFn = msg => {
    if (msg === 'already granted' || msg === 'granted') {
        // 随时可以调用通知
       return notificationClass.userAgreed();
    } else if (msg === 'close') {
        // 请求权限通知被关闭
        return notificationClass.initNotification(userSelectFn); // 再次调用
    } else if(msg === 'denied' || msg === 'already denied') {
        // 请求权限当前被拒绝 || 曾经被拒绝
        if (msg === "denied") {
            console.log("您刚刚拒绝显示通知 请在设置中更改设置");
        }else{
            console.log("您曾级拒绝显示通知 请在设置中更改设置");
        }
    }
};
notificationClass.initNotification(userSelectFn); // 请求授权

5. 显示通知

当用户同意的时候(请求授权的第一个判断),就可以在合适的时间,调用下面的方法来显示通知。

我们可以先请求用户授权,然后在需要的时候再发送通知,微博就是这么做的。

notificationClass.userAgreed();

6. 插件提供功能

  1. 不自动关闭的通知自定义时间后自动关闭
  2. 多个通知下,一次性关闭所有通知
  3. 更新通知配置,更方便的发布多个通知

插件wiki文档

  1. 插件API
  2. 插件数据
  3. notification浏览器桌面通知:关于使用notification,有可能会遇到的一些问题
  4. 更新日志

栗子:

.vue文件

求Star

如果觉得还挺好用的,可以给我点个Star