|
@@ -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);
|
|
|
+});
|