React Native配置环境后启动安卓推送通知报参数错误
React Native 推送模块启动闪退问题排查与解决
问题复现流程
- 完成项目开发环境搭建:依次安装Android Studio 4.1、Java 8、Node.js,配置系统环境变量,创建项目所需的Android虚拟设备
- 启动Android模拟器,在Android Studio内置终端执行依赖安装命令:
npm i - 执行应用启动命令:
npx react-native run-android - 应用正常安装到模拟器后启动,运行数秒自动闪退,弹出原生报错:rnpushnotification.requestpermissions got 1 arguments, expected 0
环境版本信息
React Native Environment Info: System: OS: Windows 10 CPU: (8) x64 Intel (R) Core (TM) i7 CPU 860 @ 2.80GHz Memory: 16.60 GB / 23.99 GB Binaries: npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: Version 4.1.0.0 AI-201.8743.12.41.6858069
- 项目核心依赖版本:
- React Native 0.59.10
- react-native-push-notification 3.3.0
- 项目推送模块核心业务代码:
import PushNotification from 'react-native-push-notification'; import DeviceInfo from 'react-native-device-info'; import { PushNotificationIOS, Platform } from 'react-native'; import { Actions } from 'react-native-router-flux'; import { store } from './../Store'; import { REGISTER_DEVICE } from './../actions/types'; import ImageService from '../services/ImageService'; class NotificationService { static configure() { PushNotification.configure({ // 设备推送token注册逻辑 onRegister: function(token) { const device = { tokenDispositivo: token.token, plataforma: token.os, imei: DeviceInfo.getUniqueID(), }; store.dispatch({ type: REGISTER_DEVICE, payload: device }); }, // 通知点击处理逻辑 onNotification: function(notification) { const type = Platform.OS === 'ios' ? notification.data.type : notification.type; const alert = Platform.OS === 'ios' ? notification.data.alert : notification.message; const title = Platform.OS === 'ios' ? notification.data.title : notification.title; switch (type) { case 'another': const url = Platform.OS === 'ios' ? notification.data.fotoPerfil.replace( /\s\s+/g, ' ' ).trim() : notification.fotoPerfil.replace( /\s\s+/g, ' ' ).trim(); const loanId = Platform.OS === 'ios' ? notification.data.idPrestamo.replace(' ','') : notification.idPrestamo.replace(' ',''); store.dispatch({ type: 'SHOW_MODAL', modalProps: { open: true, title: title, message: alert, url: ImageService.getUrlFormat(url), backdrop: false, closeModal: () => { store.dispatch({ type: 'HIDE_MODAL' }); PushNotification.cancelAllLocalNotifications(); Actions.summaryLoanAccepted({loanId}); } }, modalType: 'approved' }); break; case 'another': store.dispatch({ type: 'SHOW_MODAL', modalProps: { open: true, backdrop: false, message: alert, title: title, closeModal: () => { store.dispatch({ type: 'HIDE_MODAL' }); const loanId = Platform.OS === 'ios' ? notification.data.idPrestamo.replace( /\s\s+/g, ' ' ).trim() : notification.idPrestamo.replace( /\s\s+/g, ' ' ).trim(); PushNotification.cancelAllLocalNotifications(); Actions.infoLoanOutstanding({ loanId: loanId }); } }, modalType: 'billError' }); break; case 'another': const urlComprobante = Platform.OS === 'ios' ? notification.data.urlComprobante.replace( /\s\s+/g, ' ' ).trim() : notification.urlComprobante.replace( /\s\s+/g, ' ' ).trim(); store.dispatch({ type: 'SHOW_MODAL', modalProps: { open: true, backdrop: false, message: alert, closeModal: () => { store.dispatch({ type: 'HIDE_MODAL' }); store.dispatch({ type: 'SHOW_MODAL', modalProps: { open: true, imagesUrl: Platform.OS === 'ios' ? [notification.data.urlComprobante.replace(' ','')] : [notification.urlComprobante.replace(' ','')], backdrop: false, closeModal: () => { Actions.reset('layoutApplicant'); store.dispatch({ type: 'HIDE_MODAL' }); }, }, modalType: 'image' }); PushNotification.cancelAllLocalNotifications(); } }, modalType: 'review' }); break; case 'another' : store.dispatch({ type: 'SHOW_MODAL', modalProps:{ open:true, message: Platform.OS === 'ios' ? notification.data.message : notification.message , closeModal:()=>{ store.dispatch({ type:'HIDE_MODAL' }) } }, modalType: 'emptyState' }) break; default: Actions.applicantHome(); } const message = store.getState(); notification.finish(PushNotificationIOS.FetchResult.NoData); PushNotification.setApplicationIconBadgeNumber(message.auth.messages); }, // Android 推送Sender ID配置 senderID: 'SenderId', // iOS 通知权限配置 permissions: { alert: true, badge: true, sound: true }, // 冗余重复的权限配置(代码块重复定义了permissions字段) permissions: { alert: true, badge: true, sound: true }, popInitialNotification: true, requestPermissions: Platform.OS === 'ios' }); } }; export default NotificationService;
根因定位
react-native-push-notification 3.3.0 存在原生模块参数不匹配的已知bug:
- 该版本JS层的
configure方法逻辑中,会直接读取配置对象的requestPermissions字段值,将其作为入参传递给原生模块的requestPermissions方法 - 但Android端原生实现的
requestPermissions方法是无参方法,不接收任何入参,一旦JS层传递参数,就会触发原生方法签名不匹配的异常,直接导致应用闪退 - 对应代码中,
requestPermissions字段的取值为布尔值Platform.OS === 'ios',恰好作为入参传给了无参的原生方法,触发报错
修复操作
- 清理代码中重复定义的
permissions字段,避免配置覆盖的潜在问题 - 调整
requestPermissions字段的配置逻辑,避免参数传递错误,二选一执行即可:- 临时修复:将原有的布尔值配置替换为函数形式,函数内部判断平台后直接调用无参的原生权限申请方法,不传递额外参数:
// 替换原有requestPermissions配置 requestPermissions: () => { if (Platform.OS === 'ios') { PushNotification.requestPermissions(); } } - 版本修复:将
react-native-push-notification升级到3.1.5稳定版,或升级到修复了该bug的3.4.0及以上版本,更新后重新执行npm i、重新打包应用即可
- 临时修复:将原有的布尔值配置替换为函数形式,函数内部判断平台后直接调用无参的原生权限申请方法,不传递额外参数:
- 额外业务问题修正:当前代码中switch的所有case值都为
another,会导致通知类型判断完全失效,需根据实际业务的通知类型值修正case匹配条件
内容的提问来源于stack exchange,提问作者j-j suarez
相关产品推荐
相关产品推荐

