| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import { invokeTauriCommand } from './helpers/tauri.js';
- // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- /**
- * Send toast notifications (brief auto-expiring OS window element) to your user.
- * Can also be used with the Notification Web API.
- *
- * 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`.
- *
- * The APIs must be added to [`tauri.allowlist.notification`](https://tauri.app/v1/api/config/#allowlistconfig.notification) in `tauri.conf.json`:
- * ```json
- * {
- * "tauri": {
- * "allowlist": {
- * "notification": {
- * "all": true // enable all notification APIs
- * }
- * }
- * }
- * }
- * ```
- * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
- * @module
- */
- /**
- * Checks if the permission to send notifications is granted.
- * @example
- * ```typescript
- * import { isPermissionGranted } from '@tauri-apps/api/notification';
- * const permissionGranted = await isPermissionGranted();
- * ```
- *
- * @since 1.0.0
- */
- async function isPermissionGranted() {
- if (window.Notification.permission !== 'default') {
- return Promise.resolve(window.Notification.permission === 'granted');
- }
- return invokeTauriCommand({
- __tauriModule: 'Notification',
- message: {
- cmd: 'isNotificationPermissionGranted'
- }
- });
- }
- /**
- * Requests the permission to send notifications.
- * @example
- * ```typescript
- * import { isPermissionGranted, requestPermission } from '@tauri-apps/api/notification';
- * let permissionGranted = await isPermissionGranted();
- * if (!permissionGranted) {
- * const permission = await requestPermission();
- * permissionGranted = permission === 'granted';
- * }
- * ```
- *
- * @returns A promise resolving to whether the user granted the permission or not.
- *
- * @since 1.0.0
- */
- async function requestPermission() {
- return window.Notification.requestPermission();
- }
- /**
- * Sends a notification to the user.
- * @example
- * ```typescript
- * import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/api/notification';
- * let permissionGranted = await isPermissionGranted();
- * if (!permissionGranted) {
- * const permission = await requestPermission();
- * permissionGranted = permission === 'granted';
- * }
- * if (permissionGranted) {
- * sendNotification('Tauri is awesome!');
- * sendNotification({ title: 'TAURI', body: 'Tauri is awesome!' });
- * }
- * ```
- *
- * @since 1.0.0
- */
- function sendNotification(options) {
- if (typeof options === 'string') {
- // eslint-disable-next-line no-new
- new window.Notification(options);
- }
- else {
- // eslint-disable-next-line no-new
- new window.Notification(options.title, options);
- }
- }
- export { isPermissionGranted, requestPermission, sendNotification };
|