liming 2 settimane fa
parent
commit
652f9befb7
6 ha cambiato i file con 66 aggiunte e 6 eliminazioni
  1. 3 0
      capacitor.config.ts
  2. 1 2
      package.json
  3. 15 1
      src/App.vue
  4. BIN
      src/assets/mp3/im.mp3
  5. 34 3
      src/utils/notifications.js
  6. 13 0
      src/utils/websocket.js

+ 3 - 0
capacitor.config.ts

@@ -26,6 +26,9 @@ const config: CapacitorConfig = {
     CapacitorUpdater: {
       autoUpdate: false, // 自行控制 
       statsUrl: '',   // 统计上报: POST /api/stats 接收 JSON(内容是插件的事件日志),返回 200 即可。
+    },
+    LocalNotifications: { 
+      sound: "" // 为空:使用默认的系统通知声音。如果未提供,则 Android 系统将发出默认声音,iOS 系统则不发出声音。
     }
   },
   // iOS专属配置

+ 1 - 2
package.json

@@ -19,8 +19,7 @@
     "preview": "vite preview"
   },
   "dependencies": {
-    "@aparajita/capacitor-biometric-auth": "^9.0.0",
-    "@capacitor-community/native-audio": "^7.0.0",
+    "@aparajita/capacitor-biometric-auth": "^9.0.0", 
     "@capacitor/android": "^7.2.0",
     "@capacitor/app": "^7.0.1",
     "@capacitor/app-launcher": "^7.0.1",

+ 15 - 1
src/App.vue

@@ -14,8 +14,9 @@
 
 <script setup>
 import { checkAndUpdate } from "@/updater/index";
-
 import { getNotchHeight } from "@/utils/statusBar";
+import { setupNotifications } from '@/utils/notifications.js';
+
 const route = useRoute();
 const router = useRouter();
 
@@ -57,6 +58,19 @@ onBeforeMount(async () => {
 
   checkAndUpdate();
 });
+
+onMounted(() => {
+  setupNotifications(1);
+  setupNotifications(2);
+  setupNotifications(3);
+  setupNotifications(4);
+  setupNotifications(5);
+  setupNotifications(6);
+  setupNotifications(7);
+  setupNotifications(8);
+  setupNotifications(9);
+  
+});
 </script>
 
 <style lang="less">

BIN
src/assets/mp3/im.mp3


+ 34 - 3
src/utils/notifications.js

@@ -1,3 +1,34 @@
-import { LocalNotifications } from '@capacitor/local-notifications';
-import { Audio } from '@capacitor-community/audio';
-import { App } from '@capacitor/app';
+import { LocalNotifications } from "@capacitor/local-notifications";
+import { App } from "@capacitor/app";
+
+async function playNotificationSound() {
+  // 创建音频对象
+  const sound = new Audio("src/assets/mp3/im.mp3");
+  await sound.play();
+}
+
+// 初始化通知监听
+export async function setupNotifications(id) {
+  // 检查权限
+  const granted = await LocalNotifications.requestPermissions();
+  if (!granted) return;
+
+  await LocalNotifications.schedule({
+    notifications: [
+      {
+        title: "新消息",
+        body: "您收到一条新消息",
+        id,
+        sound: "", // Android无需扩展名,iOS需要(如"notification.caf")
+        extra: { messageId: "123" },
+      },
+    ],
+  });
+
+  // 播放自定义声音
+  await playNotificationSound();
+}
+// App 状态变化时处理
+App.addListener("appStateChange", ({ isActive }) => {
+  console.log("App active:", isActive);
+});

+ 13 - 0
src/utils/websocket.js

@@ -0,0 +1,13 @@
+let socket;
+
+export function initWebSocket() {
+  socket = new WebSocket('wss://your-im-server.com');
+
+  socket.onmessage = (event) => {
+    const message = JSON.parse(event.data);
+    showLocalNotification(message);
+  };
+
+  // 断线重连逻辑
+  socket.onclose = () => setTimeout(initWebSocket, 5000);
+}