notification.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { invokeTauriCommand } from './helpers/tauri.js';
  2. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  3. // SPDX-License-Identifier: Apache-2.0
  4. // SPDX-License-Identifier: MIT
  5. /**
  6. * Send toast notifications (brief auto-expiring OS window element) to your user.
  7. * Can also be used with the Notification Web API.
  8. *
  9. * This package is also accessible with `window.__TAURI__.notification` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  10. *
  11. * The APIs must be added to [`tauri.allowlist.notification`](https://tauri.app/v1/api/config/#allowlistconfig.notification) in `tauri.conf.json`:
  12. * ```json
  13. * {
  14. * "tauri": {
  15. * "allowlist": {
  16. * "notification": {
  17. * "all": true // enable all notification APIs
  18. * }
  19. * }
  20. * }
  21. * }
  22. * ```
  23. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  24. * @module
  25. */
  26. /**
  27. * Checks if the permission to send notifications is granted.
  28. * @example
  29. * ```typescript
  30. * import { isPermissionGranted } from '@tauri-apps/api/notification';
  31. * const permissionGranted = await isPermissionGranted();
  32. * ```
  33. *
  34. * @since 1.0.0
  35. */
  36. async function isPermissionGranted() {
  37. if (window.Notification.permission !== 'default') {
  38. return Promise.resolve(window.Notification.permission === 'granted');
  39. }
  40. return invokeTauriCommand({
  41. __tauriModule: 'Notification',
  42. message: {
  43. cmd: 'isNotificationPermissionGranted'
  44. }
  45. });
  46. }
  47. /**
  48. * Requests the permission to send notifications.
  49. * @example
  50. * ```typescript
  51. * import { isPermissionGranted, requestPermission } from '@tauri-apps/api/notification';
  52. * let permissionGranted = await isPermissionGranted();
  53. * if (!permissionGranted) {
  54. * const permission = await requestPermission();
  55. * permissionGranted = permission === 'granted';
  56. * }
  57. * ```
  58. *
  59. * @returns A promise resolving to whether the user granted the permission or not.
  60. *
  61. * @since 1.0.0
  62. */
  63. async function requestPermission() {
  64. return window.Notification.requestPermission();
  65. }
  66. /**
  67. * Sends a notification to the user.
  68. * @example
  69. * ```typescript
  70. * import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/api/notification';
  71. * let permissionGranted = await isPermissionGranted();
  72. * if (!permissionGranted) {
  73. * const permission = await requestPermission();
  74. * permissionGranted = permission === 'granted';
  75. * }
  76. * if (permissionGranted) {
  77. * sendNotification('Tauri is awesome!');
  78. * sendNotification({ title: 'TAURI', body: 'Tauri is awesome!' });
  79. * }
  80. * ```
  81. *
  82. * @since 1.0.0
  83. */
  84. function sendNotification(options) {
  85. if (typeof options === 'string') {
  86. // eslint-disable-next-line no-new
  87. new window.Notification(options);
  88. }
  89. else {
  90. // eslint-disable-next-line no-new
  91. new window.Notification(options.title, options);
  92. }
  93. }
  94. export { isPermissionGranted, requestPermission, sendNotification };