React Native项目报错:TypeError: null is not an object (evaluating 'RNPushNotification.getInitialNotification')
解决React Native中
RNPushNotification.getInitialNotification报错问题 可能原因及修复步骤
1. 确认模块安装完整性
先卸载并重新安装react-native-push-notification,确保依赖无损坏:
npm uninstall react-native-push-notification npm install react-native-push-notification
同时检查项目是否安装了该模块的必填依赖,比如react-native-device-info(部分版本要求),缺失的话一并安装。
2. 验证模块链接状态
React Native 0.60+支持自动链接,但偶尔会出现链接失效的情况:
- 手动触发链接:
npx react-native link react-native-push-notification - 检查Android项目配置:
打开android/app/src/main/java/[你的包名]/MainApplication.java,确认RNPushNotificationPackage已加入包列表:
打开import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage; @Override protected List<ReactPackage> getPackages() { return Arrays.asList( new MainReactPackage(), new ReactNativePushNotificationPackage() // 确保这一行存在 ); }android/settings.gradle,确认包含以下内容:include ':react-native-push-notification' project(':react-native-push-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-notification/android')
3. 补全AndroidManifest.xml配置
确保推送相关的权限、服务和接收器已配置:
- 添加权限到
<manifest>标签内:<uses-permission android:name="android.permission.WAKE_LOCK" /> <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> - 添加服务和接收器到
<application>标签内:<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationReceiver" android:exported="false" > <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </receiver> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/> </intent-filter> </receiver> <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
4. 清理缓存并重新构建
- 清理Android项目缓存:
cd android && ./gradlew clean cd .. && npm run android - 若问题仍存在,删除依赖文件后重装:
rm -rf node_modules package-lock.json npm install npm run android
5. 检查初始化时机
确保调用getInitialNotification前,PushNotification已完成初始化,在项目入口文件(如App.js)中按顺序执行:
import PushNotification from 'react-native-push-notification'; // 先完成初始化配置 PushNotification.configure({ onNotification: function(notification) { console.log('收到通知:', notification); }, requestPermissions: true, }); // 再调用getInitialNotification PushNotification.getInitialNotification((notification) => { if (notification) { console.log('启动时的初始通知:', notification); } });
内容的提问来源于stack exchange,提问作者abhishek_maran
相关产品推荐
相关产品推荐

