【问题标题】:(RN) Possible Unhandled Promise Rejection (id: 0): Push Notification(RN) 可能未处理的 Promise Rejection (id: 0):推送通知
【发布时间】:2025-12-10 09:35:01
【问题描述】:

我是 React Native 的新手。我正在使用“推送通知”发送指向该应用程序的链接。当我测试它时,一切都在后台和应用程序关闭时工作。但是每次打开应用程序时,都会收到以下警告。我该如何解决这种情况?

WARN 可能的未处理 Promise Rejection (id: 0): TypeError: null 不是对象(评估'remoteMessage.data')

我的代码:

import React, { useEffect } from 'react'
import { Button, Linking, View } from 'react-native'

import styles from './styles/SplashStyles'
import messaging from '@react-native-firebase/messaging'
import { DASHBOARD } from '../navigation/routesNames'



const Splash = ({ navigation }) => {

    useEffect(async () => {

        messaging()
            .subscribeToTopic('users')
            .then(() => console.log('Subscribed to topic!'));

        //Notification caused app to open from background state:'
        messaging()
            .onNotificationOpenedApp(remoteMessage => {
                if (remoteMessage.data.url) {
                    Linking.openURL(remoteMessage.data.url)
                }
            });

        //Notification caused app to open from quit state
        messaging()
            .getInitialNotification()
            .then(remoteMessage => {
                if (remoteMessage.data.url) {
                    Linking.openURL(remoteMessage.data.url)
                }
            });
    }, [])


    return (
        <View style={styles.container}>
            <Button
                title={"Start"}
                onPress={() => {
                    navigation.reset({
                        index: 0,
                        routes: [{ name: DASHBOARD }]
                    })
                }}
            />
        </View>
    )
}

export default Splash

【问题讨论】:

    标签: firebase react-native push-notification


    【解决方案1】:

    我认为是因为在初始化时,remoteMessage 的值在通过remoteMessage.data 专门渲染时尚未定义,导致错误。如果你不介意引入对 lodash 包的依赖,可以使用一个名为“has”的助手来检查路径是否存在。例如:

    if (_.has(remoteMessage, 'data.url') { ....
    

    【讨论】: