You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo SDK 49通知问题:应用被杀后点击无法跳转指定页面

解决Expo SDK 49中应用被杀状态下通知点击跳转失效问题

核心原因

应用处于被杀状态时,useLastNotificationResponse无法捕获通知点击事件——因为此时App是重新启动的,必须在App初始化阶段处理启动参数里的通知数据,而不是依赖组件内的Hook。

具体修复步骤

  1. 在App启动时获取启动通知数据
    在根组件(如App.tsx/App.js)的最外层,使用Notifications.getInitialNotification()方法,专门用于获取App从被杀状态启动时的通知信息:

    import { Notifications } from 'expo-notifications';
    import { NavigationContainer } from '@react-navigation/native';
    import { createStackNavigator } from '@react-navigation/stack';
    import React from 'react';
    
    const Stack = createStackNavigator();
    
    export default function App() {
      const navigationRef = React.useRef();
    
      // 初始化时检查启动通知
      React.useEffect(() => {
        async function checkInitialNotification() {
          const initialNotification = await Notifications.getInitialNotification();
          if (initialNotification) {
            // 根据通知data中的参数跳转指定页面
            const { screen } = initialNotification.request.content.data;
            if (screen) {
              navigationRef.current?.navigate(screen);
            }
          }
        }
        checkInitialNotification();
      }, []);
    
      return (
        <NavigationContainer ref={navigationRef}>
          <Stack.Navigator>
            {/* 你的页面路由配置 */}
          </Stack.Navigator>
        </NavigationContainer>
      );
    }
    
  2. 确保通知Payload携带跳转参数
    发送通知时,必须在data字段中携带目标页面标识,示例Payload:

    {
      "to": "EXPO_PUSH_TOKEN",
      "sound": "default",
      "title": "测试通知",
      "body": "点击跳转详情页",
      "data": {
        "screen": "DetailScreen"
      }
    }
    
  3. 验证权限与配置

    • 检查app.json中的通知配置:
      {
        "expo": {
          "ios": {
            "infoPlist": {
              "UIBackgroundModes": ["remote-notification"]
            }
          },
          "android": {
            "permissions": ["POST_NOTIFICATIONS"],
            "useNextNotificationsApi": true
          }
        }
      }
      
    • Android端确认开启useNextNotificationsApi,iOS端确保UIBackgroundModes包含remote-notification。
  4. 保留后台/前台的现有逻辑
    继续使用useLastNotificationResponse处理后台/前台状态下的跳转,和初始化逻辑形成互补:

    import { useLastNotificationResponse } from 'expo-notifications';
    
    function SomeComponent({ navigation }) {
      const lastNotificationResponse = useLastNotificationResponse();
      React.useEffect(() => {
        if (lastNotificationResponse) {
          const { screen } = lastNotificationResponse.notification.request.content.data;
          if (screen) {
            navigation.navigate(screen);
          }
        }
      }, [lastNotificationResponse, navigation]);
    }
    

关键注意点

  • getInitialNotification()仅在App从被杀状态启动时返回数据,后台唤醒时不会触发,和useLastNotificationResponse覆盖不同场景
  • 必须正确初始化导航引用navigationRef,否则无法在App初始化阶段执行跳转

内容的提问来源于stack exchange,提问作者AnhTp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 19:42:33