Contribute
This is a stub and is waiting for contributions. Get involved by visiting us on GitHub or joining us on Discord.
Send native notifications to your user using the notification plugin.
Install the notifications plugin to get started.
Use your project’s package manager to add the dependency:
npm run tauri add notification
yarn run tauri add notification
pnpm tauri add notification
bun tauri add notification
cargo tauri add notification
Run the following command in the src-tauri
folder to add the plugin to the project’s dependencies in Cargo.toml
:
cargo add tauri-plugin-notification
Modify lib.rs
to initialize the plugin:
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_notification::init()) .run(tauri::generate_context!()) .expect("error while running tauri application");}
If you’d like to use notifications in JavaScript then install the npm package as well:
npm install @tauri-apps/plugin-notification
yarn add @tauri-apps/plugin-notification
pnpm add @tauri-apps/plugin-notification
bun add @tauri-apps/plugin-notification
The notification plugin is available in both JavaScript and Rust.
Here are a few examples of how to use the notification plugin:
To send notifications to the user, appropriate permissions must be obtained beforehand.
Check if permission is granted
use tauri_plugin_notification::NotificationExt;
#[tauri::command]fn send_notification(app_handle: tauri::AppHandle) { // Do you have permission to send a notification? let mut state = app_handle.notification().permission_state().unwrap();}
import isPermissionGranted from '@tauri-apps/plugin-notification';
// Do you have permission to send a notification?let permissionGranted = await isPermissionGranted();
Request permission if not granted
use tauri_plugin_notification::NotificationExt;use tauri_plugin_notification::PermissionState;
#[tauri::command]fn send_notification(app_handle: tauri::AppHandle) { // Do you have permission to send a notification? let mut state = app_handle.notification().permission_state().unwrap();
// If permissions are not already granted, we will need to request them. if state != PermissionState::Granted { state = app_handle.notification().request_permission().unwrap(); }}
import { isPermissionGranted, requestPermission,} from '@tauri-apps/plugin-notification';
// Do you have permission to send a notification?let permissionGranted = await isPermissionGranted();
// If permissions are not already granted, we will need to request them.if (!permissionGranted) { const permission = await requestPermission(); permissionGranted = permission === 'granted';}
After ensuring the user has granted permissions to receive notifications, you can use the sendNotification
API:
import { isPermissionGranted, requestPermission, sendNotification,} from '@tauri-apps/plugin-notification';// when using `"withGlobalTauri": true`, you may use// const { isPermissionGranted, requestPermission, sendNotification } = window.__TAURI_PLUGIN_NOTIFICATION__;
// Do you have permission to send a notification?let permissionGranted = await isPermissionGranted();
// If not we need to request itif (!permissionGranted) { const permission = await requestPermission(); permissionGranted = permission === 'granted';}
// Once permission has been granted we can send the notificationif (permissionGranted) { sendNotification({ title: 'Tauri', body: 'Tauri is awesome!' });}
tauri::Builder::default() .plugin(tauri_plugin_notification::init()) .setup(|app| { use tauri_plugin_notification::NotificationExt; app.notification() .builder() .title("Tauri") .body("Tauri is awesome") .show() .unwrap();
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");
Contribute
This is a stub and is waiting for contributions. Get involved by visiting us on GitHub or joining us on Discord.
Contribute
This is a stub and is waiting for contributions. Get involved by visiting us on GitHub or joining us on Discord.
Contribute
This is a stub and is waiting for contributions. Get involved by visiting us on GitHub or joining us on Discord.
Aside from normal sanitization procedures of user input there are currently no known security considerations.
This permission set configures which notification features are by default exposed.
It allows all notification related features.
allow-is-permission-granted
allow-request-permission
allow-notify
allow-register-action-types
allow-register-listener
allow-cancel
allow-get-pending
allow-remove-active
allow-get-active
allow-check-permissions
allow-show
allow-batch
allow-list-channels
allow-delete-channel
allow-create-channel
allow-permission-state
Identifier | Description |
---|---|
|
Enables the batch command without any pre-configured scope. |
|
Denies the batch command without any pre-configured scope. |
|
Enables the cancel command without any pre-configured scope. |
|
Denies the cancel command without any pre-configured scope. |
|
Enables the check_permissions command without any pre-configured scope. |
|
Denies the check_permissions command without any pre-configured scope. |
|
Enables the create_channel command without any pre-configured scope. |
|
Denies the create_channel command without any pre-configured scope. |
|
Enables the delete_channel command without any pre-configured scope. |
|
Denies the delete_channel command without any pre-configured scope. |
|
Enables the get_active command without any pre-configured scope. |
|
Denies the get_active command without any pre-configured scope. |
|
Enables the get_pending command without any pre-configured scope. |
|
Denies the get_pending command without any pre-configured scope. |
|
Enables the is_permission_granted command without any pre-configured scope. |
|
Denies the is_permission_granted command without any pre-configured scope. |
|
Enables the list_channels command without any pre-configured scope. |
|
Denies the list_channels command without any pre-configured scope. |
|
Enables the notify command without any pre-configured scope. |
|
Denies the notify command without any pre-configured scope. |
|
Enables the permission_state command without any pre-configured scope. |
|
Denies the permission_state command without any pre-configured scope. |
|
Enables the register_action_types command without any pre-configured scope. |
|
Denies the register_action_types command without any pre-configured scope. |
|
Enables the register_listener command without any pre-configured scope. |
|
Denies the register_listener command without any pre-configured scope. |
|
Enables the remove_active command without any pre-configured scope. |
|
Denies the remove_active command without any pre-configured scope. |
|
Enables the request_permission command without any pre-configured scope. |
|
Denies the request_permission command without any pre-configured scope. |
|
Enables the show command without any pre-configured scope. |
|
Denies the show command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT