electron之win/mac通知免打扰

目录

系统区别

win:不支持桌面通知,使用气泡显示

[mac:有镜像/共享屏幕时 通知免打扰设置](#mac:有镜像/共享屏幕时 通知免打扰设置)

代码

Vuex:免打扰状态

src/store/App/mutations.ts

src/store/App/state.ts

src/views/miracast/index.vue

Util

【可选】src/util/ipcUtil.ts:重写ipc【日志记录】

src/util/webimutil.ts:判断系统、通知

通知

样式

html

css

参考链接


系统区别

win:不支持桌面通知,使用气泡显示

window.Electron && window.Electron.ipcRenderer.send()

mac:有镜像/共享屏幕时 通知免打扰设置

const notification =new Notification(title,option)

且通知样式是固定的,只能传部分参数

/**

* title: "字符串"

* option: {

* body: "xxxxx",

* tag: "IM",

* icon: ""

* }

*/

new后会在用户设备的通知中心中创建一个通知条目,

notification.show();手动显示通知,

但一般是把条目按需放在通知中心,等待显示

代码

Vuex:免打扰状态

src/store/App/mutations.ts

javascript 复制代码
    SET_NOTIFY_MUTED(state:any, value:any) {
        state.notifyMuted= value;
    },

src/store/App/state.ts

javascript 复制代码
    notifyMuted:false,//通知免打扰

src/views/miracast/index.vue

javascript 复制代码
  <el-checkbox v-model="isMuted" @change="toggleMute">投屏后不弹出新消息提示</el-checkbox>


    watch:{
        castStatus(newStatus:string){
            if(newStatus=='isCasting'&&this.isMuted){
                this.$store.commit('App/SET_NOTIFY_MUTED',true);
            }else{
                this.$store.commit('App/SET_NOTIFY_MUTED',false);
            }
        }
    },
  methods:{
        toggleMute() {     
            if(this.castStatus=='isCasting'&&this.isMuted){
                this.$store.commit('App/SET_NOTIFY_MUTED',true);
            }else{
                this.$store.commit('App/SET_NOTIFY_MUTED',false);
            }
        },
}

Util

【可选】src/util/ipcUtil.ts:重写ipc【日志记录】

src/util/webimutil.ts:判断系统、通知

javascript 复制代码
import ipcUtil from '@/util/ipcUtil';

const userAgent = window.navigator.userAgent.toLowerCase();
const ipc = window.Electron && window.Electron.ipcRenderer;

/**
 * @desc 判断系统类型
 */
export const checkWin = () => {
	let sUserAgent = navigator.userAgent;
	let isWin = sUserAgent.indexOf('Windows') > -1;
	return isWin;
};

//在浏览器环境中,window 是全局对象,不需要前缀
export const isMac() = () => {
    return /macintosh|mac os x/i.test(navigator.userAgent);
},
export class NotificationHelper {
	static isNotificationSupported() {
		return typeof Notification === 'function' || typeof Notification === 'object';
	}
	static requestPermission() {
		if (!NotificationHelper.isNotificationSupported()) {
			return;
		}
		Notification.requestPermission().then(function(permission) {
			console.log('用户是否允许通知: ', permission === 'granted' ? '允许' : '拒绝');
		});
	}
	static showNotification(title: any, option: any, content: any) {
    //(正在聚焦、没有隐藏、主会话页)|| 免打扰  时 不通知
		if ((store.state.App.appIsFocus && !document.hidden && content.$route.path == '/main/conversation')||store.state.App.notifyMuted) {
			return;
		}
		// win不支持桌面通知
		if (checkWin()) {
            //气泡显示
			ipc.send('display-balloon', title, option);
            //闪烁图标
			ipc.send('flash-dock');
			return;
		}
		if (!NotificationHelper.isNotificationSupported()) {
			return;
		}
		if (Notification.permission !== 'granted') {
			NotificationHelper.requestPermission();
		}
		/**
		 * title: "字符串"
		 * option: {
		 *  body: "xxxxx",
		 *  tag: "IM",
		 *  icon: ""
		 * }
		 */
		let notify = new Notification(title, {
			...option,
			silent: true, //是否保持静默
		});
		notify.onshow = function() {};
		notify.onclick = function() {};
		notify.onclose = function() {};
		notify.onclose = function() {};
		notify.onerror = function() {};
    }
}

通知

javascript 复制代码
	webimutil.NotificationHelper.showNotification(
				'视频会议',
				{
					icon: 'assets/img/meishi.ico',
					body: '您的账号在其他地方登录!',
					silent: true,
				},
				this,
			);

样式

html

html 复制代码
  <el-checkbox v-model="isMuted" @change="toggleMute">投屏后不弹出新消息提示</el-checkbox>
                <el-popover
                          v-if="!isWin"
                          placement="top-end"
                          trigger="hover"     
                          popper-class="info-popover"
                        >
                        <div class="mute-tip">
                            <dl>
                            <dd>
				                没有弹出新消息提示,请前往设置
			                </dd>
                            <dd>
				                [设置]-[通知]-[当镜像或共享屏幕时允许通知]
			                </dd>
                            </dl>
                            <img src="../../assets/img/miracast/mac_mute.png" alt="示例图片" >
                        </div>
                        <i slot="reference" class="el-icon-info"></i>
                </el-popover>

css

css 复制代码
    .info-popover{
        width: 328px;
        height: 206px ;
        background-color: rgba(255, 255, 255, 1);
        border: 1px solid rgba(206, 212, 224, 1);
        border-radius: 6px;
        box-shadow: 0px 4px 12px 0px rgba(27, 28, 30, 0.1);
        .mute-tip{
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            height: 96%;                   
            text-align: center;
            align-items: center;
            dl{
                padding-top: 7px;
            }
            dd {
                font-size: 14px;
                font-weight: 400;
                line-height: 26px;
                color: #A8ACB3;
            }
            img {
                padding: 7px 0;
                width: 296px;
                height: 110px;
                border-radius: 8px;
            }
        }
    }

参考链接

通知 | Electron

通知(Notifications) | Electron

Notifications | Apple Developer Documentation

相关推荐
布兰妮甜7 分钟前
CSS Houdini 与 React 19 调度器:打造极致流畅的网页体验
前端·css·react.js·houdini
小小愿望19 分钟前
ECharts 实战技巧:揭秘 X 轴末项标签 “莫名加粗” 之谜及破解之道
前端·echarts
两码事27 分钟前
告别繁琐的飞书表格API调用,让飞书表格操作像操作Java对象一样简单!
java·后端
小小愿望27 分钟前
移动端浏览器中设置 100vh 却出现滚动条?
前端·javascript·css
fail_to_code28 分钟前
请不要再只会回答宏任务和微任务了
前端
摸着石头过河的石头28 分钟前
taro3.x-4.x路由拦截如何破?
前端·taro
lpfasd12337 分钟前
开发Chrome/Edge插件基本流程
前端·chrome·edge
练习前端两年半1 小时前
🚀 Vue3 源码深度解析:Diff算法的五步优化策略与最长递增子序列的巧妙应用
前端·vue.js
灵魂猎手1 小时前
2. MyBatis 参数处理机制:从 execute 方法到参数流转全解析
java·后端·源码
烛阴1 小时前
TypeScript 接口入门:定义代码的契约与形态
前端·javascript·typescript