Browse Source

use node js to show notification

MaysWind 3 years ago
parent
commit
fd1f0db1a5

+ 3 - 0
app/scripts/services/ariaNgNativeElectronService.js

@@ -90,6 +90,9 @@
             showTextboxContextMenu: function (context) {
                 invokeMainProcessMethod('render-show-textbox-context-menu', context);
             },
+            showSystemNotification: function (context) {
+                invokeMainProcessMethod('render-show-system-notification', context);
+            },
             setApplicationMenu: function () {
                 invokeMainProcessMethod('render-update-app-menu-label', {
                     AboutAriaNgNative: ariaNgLocalizationService.getLocalizedText('menu.AboutAriaNgNative'),

+ 15 - 31
app/scripts/services/ariaNgNotificationService.js

@@ -1,50 +1,34 @@
 (function () {
     'use strict';
 
-    angular.module('ariaNg').factory('ariaNgNotificationService', ['$window', 'Notification', 'ariaNgLocalizationService', 'ariaNgSettingService', function ($window, Notification, ariaNgLocalizationService, ariaNgSettingService) {
-        var isSupportBrowserNotification = !!$window.Notification;
+    angular.module('ariaNg').factory('ariaNgNotificationService', ['$window', 'Notification', 'ariaNgLocalizationService', 'ariaNgSettingService', 'ariaNgNativeElectronService', function ($window, Notification, ariaNgLocalizationService, ariaNgSettingService, ariaNgNativeElectronService) {
+        var nativeNotificationPermission = 'granted';
+        var isSupportBrowserNotification = true;
 
         var isBrowserNotifactionGranted = function (permission) {
             return permission === 'granted';
         };
 
         var getBrowserNotifactionPermission = function () {
-            if (!$window.Notification) {
-                return null;
-            }
-
-            return $window.Notification.permission;
+            return nativeNotificationPermission;
         };
 
         var requestBrowserNotifactionPermission = function (callback) {
-            if (!$window.Notification) {
-                return;
-            }
+            var permission = nativeNotificationPermission;
 
-            $window.Notification.requestPermission(function (permission) {
-                if (callback) {
-                    callback({
-                        granted: isBrowserNotifactionGranted(permission),
-                        permission: permission
-                    });
-                }
-            });
+            if (callback) {
+                callback({
+                    granted: isBrowserNotifactionGranted(permission),
+                    permission: permission
+                });
+            }
         };
 
         var showBrowserNotifaction = function (title, options) {
-            if (!$window.Notification) {
-                return;
-            }
-
-            if (!isBrowserNotifactionGranted(getBrowserNotifactionPermission())) {
-                return;
-            }
-
-            options = angular.extend({
-                icon: 'tileicon.png'
-            }, options);
-
-            new $window.Notification(title, options);
+            ariaNgNativeElectronService.showSystemNotification({
+                title: title,
+                body: options.body
+            });
         };
 
         var notifyViaBrowser = function (title, content, options) {

+ 19 - 0
main/components/notification.js

@@ -0,0 +1,19 @@
+'use strict';
+
+const electron = require('electron');
+
+const localfs = require('../lib/localfs');
+
+let showNotification = function (title, body) {
+    const notification = new electron.Notification({
+        title: title,
+        body: body,
+        icon: localfs.getPackageIconPath('AriaNg.ico')
+    });
+
+    notification.show();
+};
+
+module.exports = {
+    showNotification: showNotification
+};

+ 5 - 0
main/ipc/main-process.js

@@ -6,6 +6,7 @@ const core = require('../core');
 const config = require('../config/config');
 const menu = require('../components/menu');
 const tray = require('../components/tray');
+const notification = require('../components/notification');
 const http = require('../lib/http');
 const websocket = require('../lib/websocket');
 const localfs = require('../lib/localfs');
@@ -78,6 +79,10 @@ ipcMain.on('render-show-textbox-context-menu', (event, context) => {
     }
 });
 
+ipcMain.on('render-show-system-notification', (event, context) => {
+    notification.showNotification(context.title, context.body);
+});
+
 ipcMain.on('render-update-app-menu-label', (event, labels) => {
     menu.setApplicationMenu({
         labels: labels

+ 5 - 0
main/lib/localfs.js

@@ -11,6 +11,10 @@ let isExists = function (fullPath) {
     return fs.existsSync(fullPath);
 };
 
+let getPackageIconPath = function (iconName) {
+    return path.join(__dirname, '../../assets/', iconName);
+};
+
 let readPackageFile = function (filePath) {
     return fs.readFileSync(path.join(__dirname, '../../app/', filePath), 'UTF-8');
 };
@@ -18,5 +22,6 @@ let readPackageFile = function (filePath) {
 module.exports = {
     getFullPath: getFullPath,
     isExists: isExists,
+    getPackageIconPath: getPackageIconPath,
     readPackageFile: readPackageFile
 };