notification.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const electron = require('electron');
  3. const localfs = require('../lib/localfs');
  4. const notificationMessageTemplates = {
  5. onDownloadComplete: {
  6. title: 'Download Completed',
  7. text: '${taskname}'
  8. },
  9. onBtDownloadComplete: {
  10. title: 'BT Download Completed',
  11. text: '${taskname}'
  12. },
  13. onDownloadError: {
  14. title: 'Download Error',
  15. text: '${taskname}'
  16. }
  17. };
  18. let setNotificationMessageTemplates = function (templates) {
  19. for (const eventName in notificationMessageTemplates) {
  20. if (!notificationMessageTemplates.hasOwnProperty(eventName)) {
  21. continue;
  22. }
  23. if (!templates[eventName]) {
  24. continue;
  25. }
  26. if (templates[eventName].title) {
  27. notificationMessageTemplates[eventName].title = templates[eventName].title;
  28. }
  29. if (templates[eventName].text) {
  30. notificationMessageTemplates[eventName].text = templates[eventName].text;
  31. }
  32. }
  33. };
  34. let getNotificationMessageTemplate = function (eventName) {
  35. return notificationMessageTemplates[eventName];
  36. };
  37. let showNotification = function (title, body, silent) {
  38. const notification = new electron.Notification({
  39. title: title,
  40. body: body,
  41. icon: localfs.getPackageIconPath('AriaNg.ico'),
  42. silent: !!silent
  43. });
  44. notification.show();
  45. };
  46. module.exports = {
  47. setNotificationMessageTemplates: setNotificationMessageTemplates,
  48. getNotificationMessageTemplate: getNotificationMessageTemplate,
  49. showNotification: showNotification
  50. };