mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-03-29 06:09:45 +08:00
🌱 发送通知
This commit is contained in:
@@ -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
33
src/utils/TGNotify.ts
Normal 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;
|
||||
Reference in New Issue
Block a user