liming 2 주 전
부모
커밋
3d44b3df9e
3개의 변경된 파일30개의 추가작업 그리고 18개의 파일을 삭제
  1. 2 1
      package.json
  2. 15 9
      src/App.vue
  3. 13 8
      src/utils/notifications.js

+ 2 - 1
package.json

@@ -19,7 +19,7 @@
     "preview": "vite preview"
   },
   "dependencies": {
-    "@aparajita/capacitor-biometric-auth": "^9.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",
@@ -39,6 +39,7 @@
     "clipboard": "^2.0.11",
     "crypto-js": "^4.2.0",
     "dayjs": "^1.11.13",
+    "howler": "^2.2.4",
     "less": "^4.3.0",
     "less-loader": "^12.3.0",
     "lodash": "^4.17.21",

+ 15 - 9
src/App.vue

@@ -60,16 +60,22 @@ onBeforeMount(async () => {
 });
 
 onMounted(() => {
-  setupNotifications(1);
-  setupNotifications(2);
-  setupNotifications(3);
-  setupNotifications(4);
-  setupNotifications(5);
-  setupNotifications(6);
-  setupNotifications(7);
-  setupNotifications(8);
-  setupNotifications(9);
   
+  // 0.5秒后执行
+  setTimeout(() => {
+     setupNotifications(2);
+  }, 500);
+  setTimeout(() => {
+     setupNotifications(3);
+  }, 500);
+   setTimeout(() => {
+     setupNotifications(4);
+  }, 500);
+   setTimeout(() => {
+     setupNotifications(5);
+  }, 500);
+  setupNotifications(5);
+  setupNotifications(6); 
 });
 </script>
 

+ 13 - 8
src/utils/notifications.js

@@ -1,12 +1,18 @@
 import { LocalNotifications } from "@capacitor/local-notifications";
 import { App } from "@capacitor/app";
+import { Howl } from "howler";
 
-async function playNotificationSound() {
-  // 创建音频对象
-  const sound = new Audio("src/assets/mp3/im.mp3");
-  await sound.play();
-}
-
+const sound = new Howl({
+  src: ["src/assets/mp3/im.mp3"],
+  html5: false, // 强制使用HTML5 Audio
+  format: ["mp3"], // 指定格式
+  preload: true, // 预加载
+  volume: 0.6, // 音量
+  pool: 16, // 可以同时播放3个实例
+  onloaderror: (id, error) => {
+    console.error("加载失败:", error);
+  },
+}); 
 // 初始化通知监听
 export async function setupNotifications(id) {
   // 检查权限
@@ -19,14 +25,13 @@ export async function setupNotifications(id) {
         title: "新消息",
         body: "您收到一条新消息",
         id,
-        sound: "", // Android无需扩展名,iOS需要(如"notification.caf")
         extra: { messageId: "123" },
       },
     ],
   });
 
   // 播放自定义声音
-  await playNotificationSound();
+  sound.play();
 }
 // App 状态变化时处理
 App.addListener("appStateChange", ({ isActive }) => {