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 ( + + 点击按钮发送本地通知 +