notification.d.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Options to send a notification.
  3. *
  4. * @since 1.0.0
  5. */
  6. interface Options {
  7. /** Notification title. */
  8. title: string;
  9. /** Optional notification body. */
  10. body?: string;
  11. /**
  12. * Optional notification icon.
  13. *
  14. * #### Platform-specific
  15. *
  16. * - **Windows**: The app must be installed for this to have any effect.
  17. *
  18. */
  19. icon?: string;
  20. /**
  21. * Optional notification sound.
  22. *
  23. * #### Platform-specific
  24. *
  25. * Each OS has a different sound name so you will need to conditionally specify an appropriate sound
  26. * based on the OS in use, 'default' represents the default system sound. For a list of sounds see:
  27. * - **Linux**: can be one of the sounds listed in {@link https://0pointer.de/public/sound-naming-spec.html}
  28. * - **Windows**: can be one of the sounds listed in {@link https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio}
  29. * but without the prefix, for example, if `ms-winsoundevent:Notification.Default` you would use `Default` and
  30. * if `ms-winsoundevent:Notification.Looping.Alarm2`, you would use `Alarm2`.
  31. * Windows 7 is not supported, if a sound is provided, it will play the default sound, otherwise it will be silent.
  32. * - **macOS**: you can specify the name of the sound you'd like to play when the notification is shown.
  33. * Any of the default sounds (under System Preferences > Sound) can be used, in addition to custom sound files.
  34. * Be sure that the sound file is copied under the app bundle (e.g., `YourApp.app/Contents/Resources`), or one of the following locations:
  35. * - `~/Library/Sounds`
  36. * - `/Library/Sounds`
  37. * - `/Network/Library/Sounds`
  38. * - `/System/Library/Sounds`
  39. *
  40. * See the {@link https://developer.apple.com/documentation/appkit/nssound | NSSound} docs for more information.
  41. *
  42. * @since 1.5.0
  43. */
  44. sound?: 'default' | string;
  45. }
  46. /** Possible permission values. */
  47. type Permission = 'granted' | 'denied' | 'default';
  48. /**
  49. * Checks if the permission to send notifications is granted.
  50. * @example
  51. * ```typescript
  52. * import { isPermissionGranted } from '@tauri-apps/api/notification';
  53. * const permissionGranted = await isPermissionGranted();
  54. * ```
  55. *
  56. * @since 1.0.0
  57. */
  58. declare function isPermissionGranted(): Promise<boolean>;
  59. /**
  60. * Requests the permission to send notifications.
  61. * @example
  62. * ```typescript
  63. * import { isPermissionGranted, requestPermission } from '@tauri-apps/api/notification';
  64. * let permissionGranted = await isPermissionGranted();
  65. * if (!permissionGranted) {
  66. * const permission = await requestPermission();
  67. * permissionGranted = permission === 'granted';
  68. * }
  69. * ```
  70. *
  71. * @returns A promise resolving to whether the user granted the permission or not.
  72. *
  73. * @since 1.0.0
  74. */
  75. declare function requestPermission(): Promise<Permission>;
  76. /**
  77. * Sends a notification to the user.
  78. * @example
  79. * ```typescript
  80. * import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/api/notification';
  81. * let permissionGranted = await isPermissionGranted();
  82. * if (!permissionGranted) {
  83. * const permission = await requestPermission();
  84. * permissionGranted = permission === 'granted';
  85. * }
  86. * if (permissionGranted) {
  87. * sendNotification('Tauri is awesome!');
  88. * sendNotification({ title: 'TAURI', body: 'Tauri is awesome!' });
  89. * }
  90. * ```
  91. *
  92. * @since 1.0.0
  93. */
  94. declare function sendNotification(options: Options | string): void;
  95. export type { Options, Permission };
  96. export { sendNotification, requestPermission, isPermissionGranted };