|
@@ -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 }) => {
|