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

iOS背景下React Native实现终止态推送跳转至指定页面方案咨询

WhatsApp-Like Terminated-State Push Navigation: How It Works & How to Implement in React Native

Great question—let’s break this down step by step, especially since you have iOS dev experience which will help with the platform-specific bits!

First: Does WhatsApp Stay Running in the Background When "Terminated"?

Short answer: No, not on iOS (and it’s tightly restricted on Android too). When you swipe WhatsApp away on iOS, it’s fully terminated. The magic comes from how push notifications are structured and how the OS handles them:

  • On iOS, Apple’s APNs lets you include custom data in the push payload. When a user taps a notification while the app is terminated, iOS launches the app and passes along the entire push payload to your app’s entry point.
  • WhatsApp uses this payload to include metadata about the specific chat/message (like a chat ID or message ID). Once the app launches, it reads this data and immediately navigates to the corresponding screen—no need for the app to run in the background beforehand.
  • On Android, while background restrictions are looser, the same core idea applies: the push payload includes deep link parameters, and the system launches the app with those parameters when the notification is tapped.

1. Strategies to Implement This in React Native

You’ll need to handle two key parts: configuring push notifications to carry deep link data and processing that data when the app launches (from terminated state). Here’s how to approach it:

Platform-Specific Setup (Leveraging Your iOS Knowledge)

iOS

  • Ensure your app has Push Notifications enabled in Xcode (Capabilities > Push Notifications).
  • In your AppDelegate.swift, override didFinishLaunchingWithOptions to extract the push payload when the app launches from a notification tap:
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Check if the app was launched from a push notification
      if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
        // Extract your custom deep link data (e.g., "chatId": "123")
        if let chatId = notification["chatId"] as? String {
          // Pass this to React Native via NotificationCenter
          NotificationCenter.default.post(name: NSNotification.Name("PushNotificationLaunched"), object: chatId)
        }
      }
      return true
    }
    

Android

  • In your AndroidManifest.xml, add an intent filter for your push notification activity to handle deep links, or configure your push service’s native code to pass payload data to React Native.

React Native Level Handling

Use a navigation library like react-navigation or react-native-navigation to handle screen navigation once you have the payload data. Example with react-navigation:

import { NavigationContainer, useLinking } from '@react-navigation/native';
import { Linking, useState, useEffect } from 'react-native';
import { createStackNavigator } from '@react-navigation/stack';

const Stack = createStackNavigator();
const navigationRef = React.createRef();

function App() {
  const [initialNavigationState, setInitialNavigationState] = useState();
  const { getInitialState } = useLinking(navigationRef, {
    prefixes: ['yourapp://'],
    config: {
      ChatScreen: 'chat/:chatId',
    },
  });

  useEffect(() => {
    // Fetch initial push payload (using your push service's SDK)
    async function getInitialPushData() {
      const pushData = await YourPushService.getInitialNotification();
      if (pushData?.chatId) {
        // Navigate directly to ChatScreen with the chatId
        navigationRef.current?.navigate('ChatScreen', { chatId: pushData.chatId });
      }
    }

    getInitialPushData();
  }, []);

  return (
    <NavigationContainer ref={navigationRef} initialState={initialNavigationState}>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="ChatScreen" component={ChatScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

2. Tools & Services That Support This Functionality

Most major push notification services let you include custom payload data, which is the key to this feature. Here are the top options for React Native:

  • OneSignal: Super popular for React Native—built-in support for handling notifications in terminated state. Use their getInitialNotification() method to retrieve the payload on launch, then trigger navigation. They handle most platform-specific setup under the hood.
  • Firebase Cloud Messaging (FCM): Google’s native service. For iOS, it wraps APNs; for Android, it uses FCM directly. You’ll handle payload parsing in native code and React Native, but it’s highly customizable.
  • Pusher Beams: Another solid option with React Native SDK support. They let you attach custom metadata to push notifications and provide callbacks to handle taps even when the app is terminated.

All these services let you attach fields like chatId or messageId to your push payloads, which your app can read on launch to trigger the correct navigation.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:13:37