-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lorenzo Lewis <[email protected]>
- Loading branch information
1 parent
1d1421f
commit b0225a4
Showing
4 changed files
with
139 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: Notification | ||
description: Send user notifications. Can also be used with the Notification Web API. | ||
--- | ||
|
||
import { Tabs, TabItem } from '@astrojs/starlight/components'; | ||
|
||
Send user notifications using the operating system's native notifications using `tauri-plugin-notification`. | ||
|
||
{/* TODO: Add demo here */} | ||
{/* ## Preview */} | ||
|
||
## Setup | ||
|
||
Install the plugin automatically using the Tauri CLI (recommended) or manually: | ||
|
||
<Tabs> | ||
<TabItem label="Automatic"> | ||
```bash | ||
tauri plugin add notification | ||
``` | ||
</TabItem> | ||
<TabItem label="Manual"> | ||
|
||
`Cargo.toml` | ||
|
||
```toml | ||
[dependencies] | ||
tauri-plugin-notification = "2.0.0-alpha" | ||
``` | ||
|
||
{/* TODO: 2.0 */} | ||
|
||
`lib.rs` | ||
|
||
```rust | ||
#[cfg_attr(mobile, tauri::mobile_entry_point)] | ||
pub fn run() { | ||
tauri::Builder::default() | ||
// Initialize the plugin | ||
.plugin(tauri_plugin_notification::init()) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} | ||
``` | ||
|
||
{/* TODO: add package.json if using node */} | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
## Usage | ||
|
||
You can use notifications in both JavaScript and Rust. Usage in both is similar where you should: | ||
|
||
1. Check if the user has granted notification permission | ||
2. Request permission if not granted | ||
3. Send the notification | ||
|
||
### JavaScript | ||
|
||
```js | ||
import { | ||
isPermissionGranted, | ||
requestPermission, | ||
sendNotification, | ||
} from '@tauri-apps/plugin-notification'; | ||
|
||
// Do you have permission to send a notification? | ||
let permissionGranted = await isPermissionGranted(); | ||
|
||
// If not we need to request it | ||
if (!permissionGranted) { | ||
const permission = await requestPermission(); | ||
permissionGranted = permission === 'granted'; | ||
} | ||
|
||
// Once permission has been granted we can send the notification | ||
if (permissionGranted) { | ||
sendNotification({ title: 'Tauri', body: 'Tauri is awesome!' }); | ||
} | ||
``` | ||
|
||
### Rust | ||
|
||
{/* TODO: Cover checking and requesting permission? */} | ||
|
||
```rust | ||
use tauri_plugin_notification::Notification; | ||
|
||
#[tauri::command] | ||
async fn notify(app: AppHandle) -> Result<(), ()> { | ||
Notification::new(&app.config().tauri.bundle.identifier) | ||
.title("Tauri") | ||
.body("Tauri is awesome!") | ||
.show() | ||
} | ||
``` | ||
|
||
{/* Could a troubleshooting/common issues section be useful to have on all of these pages? */} |
This file was deleted.
Oops, something went wrong.