From 5b23951643888cc07ff1b4a64a8d0c08399251aa Mon Sep 17 00:00:00 2001 From: jinyaqiu Date: Mon, 14 Jul 2025 20:12:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=9A=E7=9F=A5=E6=9D=83=E9=99=90+?= =?UTF-8?q?=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(tabs)/index.tsx | 3 +- components/message-push.tsx | 76 +++++++++++++++++++++++++ components/owner/setting.tsx | 20 ++++++- components/owner/utils.ts | 107 +++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 components/message-push.tsx diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index c55b375..2b25540 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -1,5 +1,6 @@ import IP from '@/assets/icons/svg/ip.svg'; import Lottie from '@/components/lottie/lottie'; +import MessagePush from '@/components/message-push'; import { useRouter } from 'expo-router'; import * as SecureStore from 'expo-secure-store'; import { useTranslation } from 'react-i18next'; @@ -35,7 +36,7 @@ export default function HomeScreen() { {"\n"} {t('auth.welcomeAwaken.back', { ns: 'login' })} - + {/* 唤醒按钮 */} ({ + shouldShowAlert: true, + shouldPlaySound: true, + shouldSetBadge: false, + shouldShowBanner: true, + shouldShowList: true, + }), +}); + +export default function MessagePush() { + const router = useRouter(); + const notificationListener = useRef(null); + const responseListener = useRef(null); + + useEffect(() => { + // 监听通知点击事件 + responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { + const data = response.notification.request.content.data; + console.log('通知被点击,数据:', data); + + // 根据通知数据跳转到指定页面 + if (data.screen === 'ask') { + router.push('/ask'); + } else if (data.screen === 'owner') { + router.push('/owner'); + } + }); + + // 清理监听器 + return () => { + if (notificationListener.current) { + Notifications.removeNotificationSubscription(notificationListener.current); + } + if (responseListener.current) { + Notifications.removeNotificationSubscription(responseListener.current); + } + }; + }, []); + const sendNotification = async () => { + // 请求通知权限 + const { status } = await Notifications.requestPermissionsAsync(); + if (status !== 'granted') { + alert('请先允许通知权限'); + return; + } + + // 调度本地通知 + await Notifications.scheduleNotificationAsync({ + content: { + title: '你有一条新消息 🎉', + body: '点击查看详情内容', + data: { screen: 'ask' }, + priority: 'high', // 关键:设置 high 或 max + }, + trigger: { + seconds: 2, // 延迟2秒显示 + type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL // 添加 type 字段 + }, // 延迟2秒显示 + }); + + alert('通知将在2秒后显示'); + }; + + return ( + + 点击按钮发送本地通知 +