【发布时间】:2019-11-29 16:32:42
【问题描述】:
我们有一个 react-native 应用,它通过 FCM 在以下环境中运行通知:
react-native: 0.59.10,
react-native-firebase: 5.5.6
所有配置都已设置,证书已配置等。直到现在,通知一直像时钟一样工作,但最近我们停止在前台接收通知。
这是NotificationsManager 代码:
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();
};
}, []);
当应用程序在后台时,通知在两个平台上都可以正常工作,但当应用程序在前台时从不显示。
我尝试设置show_on_foreground: "true",但没有帮助。实际上,我尝试在 receiveForegroundPN 方法中记录任何内容,但它从未被调用,也就是说,当应用程序运行时,它看起来好像从未从 firebase 收到通知。
这可能是什么问题以及使通知起作用的可能解决方案?
更新
突然之间,在发布此消息的一小时内,iOS 开始收到前台通知。 Android 仍然无法运行,但它曾经使用过。
【问题讨论】:
标签: firebase react-native push-notification react-native-firebase