我正在尝试使用react-native-firebase在本地使用react native捕获通知。
[如果应用程序在后台,则通知只会按应有的方式显示一次(显然是因为该通知未在应用程序内处理)。但是,当应用程序处于前台时,捕获通知的回调函数会以相当随机的方式多次触发。如果我刷新该应用程序然后再次发送,则该数字将呈指数增长,并且可以达到12倍或更多。
我非常简单的代码:
import React, { Component } from 'react';
import {
View,
Text,
} from 'react-native';
import messaging from '@react-native-firebase/messaging';
class App extends Component {
async componentDidMount() {
messaging().onMessage(async message => console.log("message received!!!")
}
render(){
return (
<View>
<Text>Notification App</Text>
</View>
)
}
}
export default App;
要发送通知,我正在使用Firebase云消息传递测试系统:
我知道我的FCM令牌ID,因此很容易。我的问题是如何使回调函数messaging()。onMessage仅触发一次]
如果在卸载应用程序组件时不删除消息传递侦听器,则您的应用程序可以具有多个侦听器,然后多次接收消息。尝试像这样编辑您的代码以在卸载组件时删除侦听器:
async componentDidMount() {
this.messageListener = messaging().onMessage(async message => console.log("message received!!!")
}
componentWillUnmount() {
this.messageListener();
}