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

React Native Android:实现点击通知跳转至指定页面

解决react-native-push-notification点击通知不触发onNotification及页面跳转问题

核心问题分析

onNotification仅在接收通知时触发、点击无响应,通常是因为未正确配置平台回调或未处理用户交互状态,同时冷启动(应用被杀死)场景下的通知点击需要额外处理。

分步解决方案

1. 基础配置修正

确保PushNotification.configure中开启关键配置:

import PushNotification from 'react-native-push-notification';
import { useNavigationContainerRef } from '@react-navigation/native';

// 全局导航引用,用于跨组件页面跳转
export const navigationRef = useNavigationContainerRef();

PushNotification.configure({
  onNotification: (notification) => {
    // 区分被动接收通知(userInteraction=false)和主动点击通知(userInteraction=true)
    if (notification.userInteraction) {
      // 从通知参数中提取目标页面路由
      const targetRoute = notification.data?.route || notification.route;
      if (targetRoute && navigationRef.isReady()) {
        navigationRef.navigate(targetRoute);
      }
    }

    // iOS必须调用finish,否则系统会重复触发通知回调
    if (Platform.OS === 'ios') {
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    }
  },
  permissions: { alert: true, badge: true, sound: true },
  popInitialNotification: true, // 关键:处理冷启动时点击通知的场景
  requestPermissions: true,
});

2. Android平台配置

修改android/app/src/main/AndroidManifest.xml,确保MainActivity能接收通知点击的Intent:

<activity
  android:name=".MainActivity"
  android:launchMode="singleTop" <!-- 避免重复创建Activity实例 -->
  android:windowSoftInputMode="adjustResize">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <!-- 添加通知点击的Intent过滤规则 -->
  <intent-filter>
    <action android:name="NOTIFICATION_CLICK_ACTION" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

3. iOS平台配置

修改ios/[项目名]/AppDelegate.m,实现通知点击的回调转发:

#import <React/RCTPushNotificationManager.h>

// 处理iOS 9及以下版本的远程通知点击
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

// 处理iOS 10+版本的通知点击
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler
{
  [RCTPushNotificationManager didReceiveNotificationResponse:response];
  completionHandler();
}

4. 冷启动场景处理

应用被杀死状态下点击通知启动,需要在App初始化时获取初始通知:

// 在App.js的useEffect钩子中添加
useEffect(() => {
  PushNotification.popInitialNotification((notification) => {
    if (notification) {
      const targetRoute = notification.data?.route || notification.route;
      if (targetRoute && navigationRef.isReady()) {
        navigationRef.navigate(targetRoute);
      }
    }
  });
}, []);

5. 推送Payload规范

确保推送的消息携带目标页面参数:

  • Android payload示例:
{
  "data": {
    "title": "新消息",
    "message": "点击查看详情",
    "route": "DetailScreen"
  },
  "to": "设备Token"
}
  • iOS payload示例:
{
  "aps": {
    "alert": {
      "title": "新消息",
      "body": "点击查看详情"
    },
    "sound": "default"
  },
  "route": "DetailScreen"
}

关键注意事项

  • 确保navigationRef已正确关联到你的导航容器,否则无法触发页面跳转
  • Android的launchMode建议设置为singleTop或singleTask,避免重复创建页面实例
  • iOS必须调用notification.finish(),否则系统会认为通知未处理,重复触发回调

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 06:00:16