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

notifee-react-native v7.8.0点击通知触发NullPointerException报错问题

解决React Native点击Notifee通知触发NullPointerException问题

问题分析

你将React Native从0.65.1升级到0.70.0后,点击通知仍出现java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference错误,核心原因是通知未配置有效的点击行为,导致Android系统无法生成合法的Intent处理点击事件。

解决方案

1. 升级Notifee到兼容RN 0.70.0的版本

RN大版本升级后,依赖库版本需同步适配。先确认@notifee/react-native版本支持RN 0.70.x,执行命令升级:

npm install @notifee/react-native@latest
# 或
yarn add @notifee/react-native@latest

2. 给通知配置pressAction参数

在displayNotification的android配置中添加pressAction,明确点击通知后的行为,让Android系统生成正确的Intent:

await notifee.displayNotification({
  title: 'Downloaded',
  body: `${newPath.split('/')[newPath.split('/').length - 1]}`,
  android: {
    channelId,
    // 添加点击行为配置
    pressAction: {
      id: 'default', // 唯一标识,用于后续监听点击事件
      launchActivity: 'MainActivity', // 指定点击后打开的主Activity
      // 若需在App内处理,可设置mainComponent(适配RN架构)
      // mainComponent: 'App'
    }
  },
  data: {
    myCustomData: 'custom officer'
  }
});

3. 检查AndroidManifest.xml配置

确保主Activity配置正确,无拦截Intent的异常设置:

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
  android:launchMode="singleTask"
  android:windowSoftInputMode="adjustResize">
  <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

4. 清理缓存并重新构建

RN升级后可能存在缓存问题,执行以下命令清理后重新编译:

# 清理npm/yarn缓存
npm cache clean --force
# 或
yarn cache clean

# 清理Android构建缓存
cd android && ./gradlew clean && cd ..

# 重新运行项目
npx react-native run-android

5. 监听通知点击事件(可选)

若需在App内处理通知点击后的自定义逻辑,可添加监听:

import notifee from '@notifee/react-native';

// 在App启动时注册监听
notifee.onForegroundEvent(({ type, detail }) => {
  if (type === notifee.EventType.PRESS) {
    // 处理点击逻辑,如跳转页面
    console.log('通知被点击', detail.notification);
  }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 09:01:06