notification.ts 1006 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { invoke } from './tauri'
  2. export interface Options {
  3. title: string
  4. body?: string
  5. icon?: string
  6. }
  7. export type PartialOptions = Omit<Options, 'title'>
  8. export type Permission = 'granted' | 'denied' | 'default'
  9. async function isPermissionGranted(): Promise<boolean | null> {
  10. if (window.Notification.permission !== 'default') {
  11. return Promise.resolve(window.Notification.permission === 'granted')
  12. }
  13. return invoke({
  14. __tauriModule: 'Notification',
  15. message: {
  16. cmd: 'isNotificationPermissionGranted'
  17. }
  18. })
  19. }
  20. async function requestPermission(): Promise<Permission> {
  21. return window.Notification.requestPermission()
  22. }
  23. function sendNotification(options: Options | string): void {
  24. if (typeof options === 'string') {
  25. // eslint-disable-next-line no-new
  26. new window.Notification(options)
  27. } else {
  28. // eslint-disable-next-line no-new
  29. new window.Notification(options.title, options)
  30. }
  31. }
  32. export { sendNotification, requestPermission, isPermissionGranted }