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

@notifee/react-native前台通知点击重启、跳转失效问题求助

问题解决:@notifee/react-native前台通知点击导致应用重启及跳转失效

核心问题分析

你当前的配置仅处理了后台状态的通知事件(notifee.onBackgroundEvent),但前台状态下的通知点击事件未专门监听,同时可能存在Android启动模式冲突,导致点击前台通知触发应用重启而非预期跳转。


解决方案步骤

1. 添加前台通知事件监听

前台状态下的通知交互(包括点击)需要通过notifee.onForegroundEvent专门监听,在你的App根组件(如App.js)中添加以下逻辑:

import { EventType } from '@notifee/react-native';
import { useEffect } from 'react';
import { useNavigation } from '@react-navigation/native'; // 假设使用React Navigation

function App() {
  const navigation = useNavigation();

  useEffect(() => {
    // 监听前台通知点击事件
    const unsubscribeForeground = notifee.onForegroundEvent(async ({ type, detail }) => {
      if (type === EventType.PRESS) {
        // 从通知自定义数据中获取跳转参数
        const targetScreen = detail.notification.data?.targetScreen;
        if (targetScreen) {
          // 执行页面跳转
          navigation.navigate(targetScreen);
        }
      }
    });

    // 组件卸载时取消监听
    return () => unsubscribeForeground();
  }, [navigation]);

  // ... 其他组件代码
}

export default App;

2. 检查Android启动模式配置

打开android/app/src/main/AndroidManifest.xml,确保主Activity的launchMode为standard或singleTop,避免使用singleTask/singleInstance(这类模式会导致点击通知时强制重启应用):

<activity
  android:name=".MainActivity"
  android:launchMode="standard" <!-- 重点:使用standard或singleTop -->
  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
  android:exported="true"
  android:windowSoftInputMode="adjustResize">
  <!-- ... 其他配置 -->
</activity>

3. 优化通知展示配置

确保onMessageReceived中的通知配置里,Android的pressAction参数配置正确,维持默认id的同时明确启动当前Activity:

async function onMessageReceived(notification) {
  const title = notification.notification?.title || '默认标题';
  const messageBody = notification.notification?.body || '默认内容';

  await notifee.displayNotification({
    id: notification.messageId,
    title: title,
    body: messageBody,
    data: notification.data,
    android: {
      channelId: 'default',
      pressAction: {
        id: 'default', // 与事件监听中的默认点击事件匹配
        launchActivity: 'default', // 确保启动当前Activity而非重启应用
      },
    }
  });
}

4. 确认初始化顺序

在index.js中保证初始化顺序正确:先初始化Firebase相关模块,再注册消息与通知监听:

import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import messaging from '@react-native-firebase/messaging';
import notifee from '@notifee/react-native';
import { EventType } from '@notifee/react-native';

// 后台消息处理
messaging().setBackgroundMessageHandler(onMessageReceived);

// 后台/被杀状态下的通知点击处理
notifee.onBackgroundEvent(async ({ type, detail }) => {
  if (type === EventType.PRESS) {
    const targetScreen = detail.notification.data?.targetScreen;
    // 这里可通过Linking或导航深层链接处理跳转
  }
});

AppRegistry.registerComponent(appName, () => App);

验证步骤

  1. 从应用托盘打开应用并保持前台状态
  2. 发送测试通知,点击后确认应用不会重启且能正常跳转
  3. 杀掉应用后从通知打开,再次测试前台通知点击行为

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 10:08:11