Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Request Notification Section #2022

Draft
wants to merge 5 commits into
base: v2
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 83 additions & 11 deletions src/content/docs/plugin/notification.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import Stub from '@components/Stub.astro';

Send native notifications to your user using the notification plugin.

## Supported Platforms

- Windows
- macOS
- Linux
- iOS
- Android

## Setup

Install the notifications plugin to get started.
Expand Down Expand Up @@ -67,42 +75,106 @@ Install the notifications plugin to get started.

## Usage

The notification plugin is available in both JavaScript and Rust.

Here are a few examples of how to use the notification plugin:

- [Send notification to users](#send-notification)
- [Send notifications](#send-notification)
- [Add an action to a notification](#actions)
- [Add an attachment to a notification](#attachments)
- [Send a notification in a specific channel](#channels)

{/* TODO: Link to which language to use, frontend vs. backend guide when it's made */}

The notification plugin is available in both JavaScript and Rust.
### Requesting Permission

### Send Notification
To send notifications to the user, appropriate permissions must be obtained beforehand.

{/* TODO: Demo component */}
<Steps>
<ol>
<li>
Check if permission is granted
<Tabs>
<TabItem label="Rust">
```rust {1} {6}
use tauri_plugin_notification::NotificationExt;

Follow these steps to send a notification:
#[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();
}
```
</TabItem>
<TabItem label="JavaScript">
```js
import isPermissionGranted from '@tauri-apps/plugin-notification';

// Do you have permission to send a notification?
let permissionGranted = await isPermissionGranted();
```
</TabItem>
</Tabs>
</li>

<li>
Request permission if not granted
<Tabs>
<TabItem label="Rust">
```rust {2} {10-12}
use tauri_plugin_notification::NotificationExt;
use tauri_plugin_notification::PermissionState;

<Steps>
1. Check if permission is granted
#[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();

2. Request permission if not granted
// If permissions are not already granted, we will need to request them.
if state != PermissionState::Granted {
state = app_handle.notification().request_permission().unwrap();
}
}
```
</TabItem>
<TabItem label="JavaScript">
```js {3} {10-13}
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';
}
```
</TabItem>
</Tabs>
</li>

</ol>
</Steps>

3. Send the notification
</Steps>
### Send Notification

<Tabs>
<TabItem label="JavaScript">

After ensuring the user has granted permissions to receive notifications, you can use the `sendNotification` API:

```javascript
import {
isPermissionGranted,
requestPermission,
sendNotification,
} from '@tauri-apps/plugin-notification';
// when using `"withGlobalTauri": true`, you may use
// const { isPermissionGranted, requestPermission, sendNotification, } = window.__TAURI_PLUGIN_NOTIFICATION__;
// const { isPermissionGranted, requestPermission, sendNotification } = window.__TAURI_PLUGIN_NOTIFICATION__;

// Do you have permission to send a notification?
let permissionGranted = await isPermissionGranted();
Expand Down
Loading