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

React Native Android前台接收FCM推送时应用崩溃问题求助

解决react-native-fcm前台接收推送崩溃的问题

我帮你分析下当前代码和配置里可能导致前台崩溃的问题,并给出对应的修复方案:

一、首要问题:重复监听通知事件

看你的React Native代码,同时在componentDidMount和全局作用域中注册了FCMEvent.Notification监听,这会导致推送事件被重复处理,很可能引发崩溃。你需要移除其中一处监听,建议保留全局的监听(或者只保留组件内的,根据你的业务需求),避免重复触发。

修改后的代码示例(移除componentDidMount内的重复监听):

componentDidMount() {
  // iOS: show permission prompt for the first call. later just check permission in user settings
  // Android: check permission in user settings
  FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));

  FCM.getFCMToken().then(token => {
    console.log(token)
    // store fcm token in your server
  });

  // 移除这里的重复监听逻辑
  // this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
  //   // optional, do some component related stuff
  // });

  FCM.getInitialNotification().then(notif => {
    console.log(notif)
  });
}

componentWillUnmount() {
  // 若保留全局监听,此处无需移除操作;若仅保留组件内监听,再保留移除逻辑
  // this.notificationListener.remove();
}

// 只保留全局的通知监听
import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm';

FCM.on(FCMEvent.Notification, async (notif) => {
  // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
  if(notif.local_notification){
    //this is a local notification
  }
  if(notif.opened_from_tray){
    //iOS: app is open/resumed because user clicked banner
    //Android: app is open/resumed because user clicked banner or tapped app icon
  }

  if(Platform.OS ==='ios'){
    switch(notif._notificationType){
      case NotificationType.Remote:
        notif.finish(RemoteNotificationResult.NewData)
        break;
      case NotificationType.NotificationResponse:
        notif.finish();
        break;
      case NotificationType.WillPresent:
        notif.finish(WillPresentNotificationResult.All)
        break;
    }
  } else {
    // Android前台接收推送时,确保通知能正确显示,避免崩溃
    // 如果推送payload未设置notification字段,手动构建本地通知
    if (!notif.local_notification && !notif.opened_from_tray) {
      FCM.presentLocalNotification({
        title: notif.notification?.title || '系统通知',
        body: notif.notification?.body || '您有一条新消息',
        priority: "high",
        show_in_foreground: true,
        local: true
      });
    }
  }
});

FCM.on(FCMEvent.RefreshToken, (token) => {
  console.log(token)
});

二、Android配置检查

  1. 验证默认通知图标:Manifest里设置的com.google.firebase.messaging.default_notification_icon为@mipmap/ic_launcher,请确保这个图标资源存在,否则前台接收推送时可能因找不到图标崩溃。
  2. 清理重复权限:你的Manifest里重复声明了android.permission.RECEIVE_BOOT_COMPLETED,建议删除其中一条,避免配置冗余。
  3. 确认服务注册有效性:MessagingService和InstanceIdService的注册格式没问题,但要确保react-native-fcm已正确链接到项目中(可通过检查原生项目依赖确认)。

三、其他排查点

  • 检查推送payload格式:前台接收的推送payload如果缺少notification或data等必要字段,可能导致代码中访问notif.notification?.title时出现空指针,建议在处理前增加空值判断。
  • 查看崩溃日志:通过Android Studio的Logcat搜索AndroidRuntime相关错误,获取具体的崩溃堆栈信息,能更精准定位问题所在。

按照以上步骤修改后,应该能解决前台接收推送崩溃的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:14:12