Skip to content

Commit

Permalink
fix: some final tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhyll committed Aug 25, 2023
1 parent 088f10b commit 2ae0335
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/components/list/Recipes.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { LinkCard, CardGrid } from '@astrojs/starlight/components';
import { type CollectionEntry, getEntry } from 'astro:content';
const entries = ['2/guide/recipe/stub'];
const entries = ['2/guide/recipe/notifications'];
const list = (await Promise.all(
entries.flatMap((entry) => getEntry('docs', entry))
Expand Down
112 changes: 112 additions & 0 deletions src/content/docs/2/guide/recipe/notifications.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: Notifications
description: Stay tuned while we work on some delicious recipes.
---

import { Tabs, TabItem } from '@astrojs/starlight/components';

In this recipe we'll take a look at sending a native notification to your user. We'll do this by using the official notification plugin.

## Setup

First you have to install the notification plugin in your backend.

<Tabs>
<TabItem label="Auto">
```bash
tauri plugin add notification
```
</TabItem>
<TabItem label="Manual">
<Tabs>
<TabItem label="Cargo.toml">
```toml
[dependencies]
tauri-plugin-notification = "2.0.0-alpha"
```
</TabItem>
<TabItem label="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");
}
```
</TabItem>
</Tabs>
</TabItem>
</Tabs>

## Usage

Once you're done with the general setup you're free to use the notification plugin in either your frontend or straight from the backend.

### Frontend

Install the `@tauri-apps/plugin-notification` package using your favorite package manager.

```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!' });
}
```

### Backend

You can also create notifications straight from the backend. All you need to do is create an instance of `Notification` and `.show()` it.

<Tabs>
<TabItem label="Command">
```rust
use tauri_plugin_notification::Notification;

#[tauri::command]
async fn notify(app: AppHandle) -> Result<(), ()> {
Notification::new(&app.config().tauri.bundle.identifier)
.title("New message")
.body("You've got a new message.")
.show()
}
```
</TabItem>
<TabItem label="Window event">
```rust
```
</TabItem>
<TabItem label="Setup function">
```rust
```
</TabItem>

</Tabs>

## Deep dive

### How it works

The API sends a signal over the IPC to the backend. After that either the `notify-rust` or `win7-notifications` crate
takes care of sending a notification to the users system.

### Security

Aside from normal sanitization procedures of user input there are currently no known security notes for this recipe.
112 changes: 0 additions & 112 deletions src/content/docs/2/recipe/notifications.mdx

This file was deleted.

0 comments on commit 2ae0335

Please sign in to comment.