利用notification显示桌面通知

利用notification显示桌面通知

在这里插入图片描述
代码如下:

<template>
    <div class="app-container">
        <h1>Notification</h1>
        <el-input
            v-model="value"
            style="width:300px;margin-right:20px;"
        />
        <el-button @click="handleClick">显示Notify</el-button>
    </div>
</template>

<script>
    export default {
        name: "index",
        data() {
            return {
                value: '新的消息',
                PERMISSON_GRANTED:'granted',

            };
        },
        methods: {
            async handleClick() {
                // 创建一个消息提示
                const notification = await this.createNotify(this.value, {
                    body: '你有新消息,请及时查看',
                    icon: '/xiaoxi.png', //public下的img的图片
                    data: 'https://www.baidu.com/',
                });

                // 处理点击事件
                if (notification) {
                    notification.onclick = function (event) {
                        console.log(event);
                    };
                }
            },
            async createNotify(title, options) {
                // 如果用户已经允许,直接显示消息,如果不允许则提示用户授权
                if (Notification.permission != this.PERMISSON_GRANTED) {
                    const res = await Notification.requestPermission(); //用于当前页面向用户申请显示通知的权限。这个方法只能被用户行为调用(比如:onclick 事件),并且不能被其他的方式调用。
                }

                if (Notification.permission == this.PERMISSON_GRANTED) {
                    return new Notification(title, options);
                } else {
                    return null;
                }
            }
        },
    }
</script>

<style scoped>

</style>

详细了解notification API点此链接进入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值