Browse Source

桌面通知

gaoshuaixing 4 năm trước cách đây
mục cha
commit
29556f9e1f

+ 42 - 1
electron/ipc/example.js

@@ -9,11 +9,13 @@
  * @param arg 接收到的消息
  */
 
-const {app, dialog, BrowserWindow, BrowserView} = require('electron');
+const {app, dialog, BrowserWindow, BrowserView, Notification} = require('electron');
 const path = require('path');
+const _ = require('lodash');
 
 let myTimer = null;
 let browserViewObj = null;
+let notificationObj = null;
 
 exports.hello = function (event, channel, msg) {
   let newMsg = msg + " +1"
@@ -124,3 +126,42 @@ exports.removeViewContent = function () {
 
   return winObj.id
 }
+
+/**
+ * 创建系统通知
+ */
+ exports.sendNotification = function (event, channel, arg) {
+  if (!Notification.isSupported()) {
+    return '当前系统不支持通知';
+  }
+
+  let options = {};
+  if (!_.isEmpty(arg.title)) {
+    options.title = arg.title;
+  }
+  if (!_.isEmpty(arg.subtitle)) {
+    options.subtitle = arg.subtitle;
+  }
+  if (!_.isEmpty(arg.body)) {
+    options.body = arg.body;
+  }
+  if (!_.isEmpty(arg.silent)) {
+    options.silent = arg.silent;
+  }
+
+  notificationObj = new Notification(options);
+
+  if (arg.clickEvent) {
+    notificationObj.on('click', (e) => {
+      let data = {
+        type: 'click',
+        msg: '您点击了通知消息'
+      }
+      event.reply(`${channel}`, data)
+    });
+  }
+
+  notificationObj.show();
+
+  return true
+}

+ 6 - 1
frontend/src/config/router.config.js

@@ -37,7 +37,12 @@ export const constantRouterMap = [
             path: '/demo/window/index',
             name: 'DemoWindowIndex',
             component: () => import('@/views/demo/window/Index')
-          },          
+          },
+          {
+            path: '/demo/notification/index',
+            name: 'DemoNotificationIndex',
+            component: () => import('@/views/demo/notification/Index')
+          },                  
           {
             path: '/demo/shortcut/index',
             name: 'DemoShortcutIndex',

+ 6 - 0
frontend/src/layouts/DemoMenu.vue

@@ -48,6 +48,12 @@ export default {
           pageName: 'DemoWindowIndex',
           params: {}
         },
+        'menu_402' : {
+          icon: 'profile',
+          title: '通知',
+          pageName: 'DemoNotificationIndex',
+          params: {}
+        },
         'menu_500' : {
           icon: 'profile',
           title: '软件',

+ 75 - 0
frontend/src/views/demo/notification/Index.vue

@@ -0,0 +1,75 @@
+<template>
+  <div id="app-demo-window-view">
+    <div class="one-block-1">
+      <span>
+        1. 弹出桌面通知(主进程)
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-space>
+        <a-button @click="sendNotification(0)">默认</a-button>
+        <a-button @click="sendNotification(1)">发出提示音</a-button>
+        <a-button @click="sendNotification(2)">点击通知触发事件</a-button>
+      </a-space>
+    </div>
+  </div>
+</template>
+<script>
+
+export default {
+  data() {
+    return {
+      views: [
+        {
+          type: 'main',
+          title: '通知标题',
+          subtitle: '副标题', // macOS系统专有属性
+          body: '这是通知内容-默认',
+          silent: true,
+          icon: '/asset/images/tray_logo.png',
+        },
+        {
+          type: 'main',
+          title: '提示音',
+          subtitle: '提示音',
+          body: '这是通知内容-提示音',
+          silent: false,
+        },
+        {
+          type: 'main',
+          title: '点击通知事件',
+          subtitle: '点击通知事件',
+          body: '这是通知内容-点击通知事件',
+          silent: false,
+          clickEvent: true
+        }       
+      ],
+    };
+  },
+  methods: {
+    sendNotification (index) {
+      const self = this;
+      self.$ipc.on('example.sendNotification', (event, result) => {
+        if (Object.prototype.toString.call(result) == '[object Object]') {
+          self.$message.info(result.msg);
+        }  
+      })
+      self.$ipc.send('example.sendNotification', this.views[index]);
+    },
+  }
+};
+</script>
+<style lang="less" scoped>
+#app-demo-window-view {
+  padding: 0px 10px;
+  text-align: left;
+  width: 100%;
+  .one-block-1 {
+    font-size: 16px;
+    padding-top: 10px;
+  }
+  .one-block-2 {
+    padding-top: 10px;
+  }
+}
+</style>