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

相关推荐
码兄科技20 小时前
Java AI智能体开发实战:从零构建智能对话系统指南
java·开发语言·人工智能
折哥的程序人生 · 物流技术专研21 小时前
第8篇:六大设计原则在工厂模式中的体现
java·设计模式·设计原则·工厂模式·编程进阶·扩充系列
小明bishe1821 小时前
计算机毕业设计之基于JAVA的植物科普网站
java·spring boot·spring·架构·课程设计
r_oo_ki_e_21 小时前
Java Map 集合学习笔记
java·笔记·学习
SL-staff1 天前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
咖啡八杯1 天前
GoF设计模式——模板方法模式
java·后端·spring·设计模式
梦帮科技1 天前
GRAVIS v4.0:基于Web的极速套利架构设计与实时数据流实现
前端·人工智能·rust·自动化·区块链·智能合约·数字货币
爱笑的源码基地1 天前
一款全开源的信创云PACS影像云平台解决方案,支持多模态医学影像处理(CT/MR/DR/超声/病理等)
java·源码·国产化·pacs·云影像·区域pacs
爱勇宝1 天前
办公资料反复修改、补传、交接混乱,我做了个桌面工具来解决这件事
前端·后端·程序员
我命由我123451 天前
Android 开发问题:ClickableSpan 的点击事件没有生效
java·java-ee·android studio·android jetpack·android-studio·android runtime