🌱 发送通知

This commit is contained in:
BTMuli
2026-01-02 00:01:30 +08:00
parent 03136c4864
commit 0288e38c95
8 changed files with 132 additions and 19 deletions

View File

@@ -19,26 +19,22 @@
</div>
</template>
<script lang="ts" setup>
import { invoke } from "@tauri-apps/api/core";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { onMounted, onUnmounted } from "vue";
let textScaleListener: UnlistenFn | null = null;
onMounted(async () => {
textScaleListener = await listen("text_scale_change", (payload) => {
console.log(payload);
});
});
onUnmounted(() => {
if (textScaleListener !== null) {
textScaleListener();
textScaleListener = null;
}
});
import showSnackbar from "@comp/func/snackbar.js";
import {
isPermissionGranted,
requestPermission,
sendNotification,
} from "@tauri-apps/plugin-notification";
async function test() {
const res = await invoke("read_text_scale");
console.log(res);
const check = await isPermissionGranted();
console.log(check);
if (!check) {
showSnackbar.warn("没有通知权限");
const permission = await requestPermission();
console.log(permission);
}
sendNotification({ title: "New Message", body: "You have a new message" });
}
</script>
<style lang="css" scoped>

33
src/utils/TGNotify.ts Normal file
View File

@@ -0,0 +1,33 @@
/**
* 系统通知简单封装
* @since Beta v.0.9.1
*/
import showSnackbar from "@comp/func/snackbar.js";
import {
isPermissionGranted,
requestPermission,
sendNotification,
} from "@tauri-apps/plugin-notification";
/**
* 展示通知
* @param title - 标题
* @param text - 内容
*/
async function showNormalNotify(title: string, text: string): Promise<void> {
const check = await isPermissionGranted();
if (!check) {
const permission = await requestPermission();
if (permission !== "granted") {
showSnackbar.warn("Notify permission is not allowed!");
return;
}
}
sendNotification({ title: title, body: text });
}
const TGNotify = {
normal: showNormalNotify,
};
export default TGNotify;