notification.cjs 3.1 KB

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