Element 学习(2):通知

前言

使用官方文档里的Notification发起通知,无论主进程还是渲染进程都没有成功。如果是哪个地方写错了,希望大佬可以指出。

可以使用node-notifier来代替Notification实现通知的功能

主进程通知

主进程通知需要使用 Notification 模块

Notification是一个EventEmitter(事件触发与事件监听器功能的封装)

静态方法

Notification.isSupported() :判断当前操作系统是否支持桌面通知,返回值是一个布尔值

Notification对象

new Notification([options]):options可选参数,是一个对象,通知的配置项。常用的属性如下:

title:		(String)标题
body:		(String)通知的主题内容
silent:	(Boolean)在显示通知时是否发出系统提示音
icon:		(String)通知图标,

事件

show:		显示通知
click:		点击通知时触发
close:		手动关闭通知时触发,这个事件不能保证在所有情况下都会触发
const { app, BrowserWindow } = require('electron')
const reloader = require('electron-reloader')
const {Notification } =require('electron')

reloader(module)

const createWindow = () => {
    //创建窗口
    const mainWindow = new BrowserWindow({
        width: 400,
        height: 400,
        webPreferences: {
            // 开启node模块
            nodeIntegration: true,
            // 开启remote模块
            enableRemoteModule: true,
            //禁用上下文隔离
            contextIsolation: false
        }
    })
    //设置可以打开调试
    mainWindow.webContents.openDevTools()
    //加载要显示的路径
    mainWindow.loadURL('http://localhost:3000/')
}

//通知
const sendNotification = () => {
    console.log("Notification:",Notification.isSupported())
    // 检查是否支持,Notification.isSupported()
    if (Notification.isSupported()) {
        let notification = new Notification({
            title: '🌸',
            body: 'Have a good day~'
        })
        console.log(notification.body)
        notification.show()
    } else {
        console.log("不支持发送通知!")
    }
}

app.whenReady().then(createWindow).then(()=>{
    sendNotification()
})

不知道是不是哪里的问题,日志可以输出,但是通知就是不显示。

node-notifier

百度了一下,如果想在主进程中发起通知可以安装node-notifier依赖

安装命令:

cnpm install --save node-notifier

node-notifier官方文档

基本使用如下:

// String
notifier.notify('Message');

//Object
const { app, BrowserWindow } = require('electron')
const reloader = require('electron-reloader')

const notifier = require('node-notifier');
const path = require('path');

reloader(module)

const createWindow = () => {
    //创建窗口
    const mainWindow = new BrowserWindow({
        width: 400,
        height: 400,
        webPreferences: {
            // 开启node模块
            nodeIntegration: true,
            // 开启remote模块
            enableRemoteModule: true,
            //禁用上下文隔离
            contextIsolation: false
        },
    })
    //设置可以打开调试
    mainWindow.webContents.openDevTools()
    //加载要显示的路径
    mainWindow.loadURL('http://localhost:3000/')
}

app.whenReady().then(createWindow).then(()=>{
    notifier.notify(
    {
        title:'消息提示', //通知的标题
        message:'Hello world!',  //通知的主体内容
        icon: path.join(__dirname, '/public/logo512.png')  ,//图标,通过path模块加载
        sound:true,  //是否显示提示音,true显示
    },
     function (err, response, metadata) {
    })
})

运行结果:
在这里插入图片描述

渲染进程通知

渲染进程的通知,对象和方法与主进程一致,有细微的区别

实例: 点击按钮发起通知

import { Button } from 'element-react'

let style1 = {
    margin: '30px'
}

//发送通知
let sendNotification = () => {
    let notification = new Notification("通知", {
        body: '通知的主体'
    })
    console.log(notification)
    notification.onshow=()=>{
        console.log("通知展示")
    }
    notification.onclick = () => {
        console.log('通知被点击后触发')
    }
}

const add = () => {
    return (
        <div style={style1}>
            <Button type="primary" onClick={sendNotification.bind(this)}>点击</Button>
        </div>
    )
}

export default add;

真的服了,不知道是不是电脑的问题。通知就是不展示。从控制台可以看到,方法执行了
在这里插入图片描述
node-notifier方式

不知道啥原因,对象可以打印出来,显示通知时直接报错。不知道是不是电脑问题,改天换台电脑试一下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无知的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值