我想处理前台firebase消息。但messaging((.onMessage并不是第一次在iOS中推出应用程序时触发的。这在安卓系统中运行良好。具体情况是:
- 首次启动应用程序:messaging((。在iOS中未触发onMessage
- 关闭并重新打开应用程序:messaging((。onMessage将触发
import { Alert } from 'react-native';
import messaging from '@react-native-firebase/messaging';
function FCMReadInForeGround() {
useEffect(() => {
const unsubscribe = messaging().onMessage(async remoteMessage => {
Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
});
return unsubscribe;
}, []);
}
export default FCMReadInForeGround;```
我希望这能帮助到你。我也有同样的问题,我正在查看ios消息设置文档。
这里有链接:https://rnfirebase.io/messaging/usage/ios-setup。
就我而言,我还没有将APN与FCM(iOS(链接
尝试在组件外部调用setBackgroundMessageHandler
。
// Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
try {
console.log('Remote notification', remoteMessage)
} catch (err) { console.log(err) }
});
您需要使用getInitialNotification
https://rnfirebase.io/messaging/notifications#handling-交互
import messaging from '@react-native-firebase/messaging';
...
useEffect(() => {
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage.notification,
);
}
});
}, []);