【发布时间】: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