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

相关推荐
噢,我明白了29 分钟前
JAVA中的一些基础应用知识点
java·开发语言
miss34 分钟前
AI 改完不敢上线?OpenSpec 规约驱动开发实战
前端·代码规范·前端工程化
SL_staff1 小时前
私有化低代码不是妥协,而是企业级数字化的自主可控主航道
java
装不满的克莱因瓶1 小时前
【SpringAI Alibaba】ReactAgent结构化输出OutputType与OutputSchema实战
java·人工智能·ai·springai·springaialibaba
AiMan1 小时前
腾讯ima还是不可以导出左侧所有对话,只能单条导出,方法不对吗
前端
去伪存真1 小时前
并发量一高就装死?Agent 全链路“防猝死”手册
前端·agent
Moment1 小时前
AI 编程时代,聊聊我的 Mac mini 双屏桌面
前端·后端
AiMan1 小时前
先确认能不能一次把Microsofat Copilot所有内容打包一个文件导出
前端
总要学点什么2 小时前
第97篇:三维数据加载-城市建筑白模自定义着色器——让一条光带沿高度扫上去,快慢却由帧率说了算
前端·gis·webgl·cesium·着色器·空间分析·白模
秋天的怀念_David2 小时前
算法一千零一夜 · 第二十八夜 注册
java·程序人生·算法