如何使用react native firebase v5正确设置前台通知?
我们有一个react本机应用程序,它通过FCM在以下环境中运行其通知:如何使用react native firebase v5正确设置前台通知?,firebase,react-native,push-notification,react-native-firebase,Firebase,React Native,Push Notification,React Native Firebase,我们有一个react本机应用程序,它通过FCM在以下环境中运行其通知: react-native: 0.59.10, react-native-firebase: 5.5.6 所有配置都已设置,证书已配置,等等。直到目前为止,通知一直像时钟一样工作,但最近我们停止在前台接收通知 以下是通知管理器代码: import firebase from 'react-native-firebase'; const CHANNEL = 'default'; ... const localNo
react-native: 0.59.10,
react-native-firebase: 5.5.6
所有配置都已设置,证书已配置,等等。直到目前为止,通知一直像时钟一样工作,但最近我们停止在前台接收通知
以下是通知管理器代码:
import firebase from 'react-native-firebase';
const CHANNEL = 'default';
...
const localNotificationListener = React.useRef();
const notificationOpenListener = React.useRef();
const onTokenRefreshListener = React.useRef();
const receiveForegroundPN = (n) => {
const notification = new firebase.notifications.Notification()
.setNotificationId(n.notificationId)
.setTitle(n.title)
.setSubtitle(n.subtitle)
.setBody(n.body)
.setData(n.data);
if (Platform.OS === 'android') {
notification
.android.setChannelId(CHANNEL)
.android.setSmallIcon('ic_launcher');
}
firebase.notifications().displayNotification(notification);
};
React.useEffect(() => {
const channel = new firebase.notifications.Android.Channel(
CHANNEL,
'Bank Notifications',
firebase.notifications.Android.Importance.Max,
).setDescription('*****');
firebase.notifications().android.createChannel(channel);
localNotificationListener.current = firebase.notifications().onNotification(receiveForegroundPN);
notificationOpenListener.current = firebase.notifications().onNotificationOpened(openNotification);
onTokenRefreshListener.current = firebase.messaging().onTokenRefresh((fcmToken) => {
registerToken({
device_token: fcmToken,
});
});
runFlow();
return () => {
localNotificationListener.current();
notificationOpenListener.current();
onTokenRefreshListener.current();
};
}, []);
当应用程序处于后台时,通知在两种平台上都可以正常工作,但当应用程序处于前台时,通知不会显示
我试过将在前景上显示设置为:“true”
,但没有效果。实际上,我尝试在receiveForegroundPN
方法中记录任何内容,但从未调用过,也就是说,当应用程序运行时,似乎从未收到来自firebase的通知
这里可能存在什么问题以及使通知生效的可能解决方案
更新
突然,在发布这篇文章的一小时内,iOS开始收到前台通知。安卓系统仍然不能工作,但它曾经工作过。我也有类似的问题,
onNotification方法没有触发,所以我尝试了onMessage方法,它似乎适用于前台
firebase.messages().onMessage(msg=>{
//…您的代码在这里
});