问题
I try to get react-native-firebase
v6 to work in my app. I use React Native 0.59.10.
I have installed react-native-firebase
v6 according to the documentation. It didn't specify about adding service MyFirebaseMessagingService
into the AndroidManifest.xml
unlike in v5 so I didn't do it. Afterwards, the app didn't receive any notification while in foreground but did receive them while in background.
I tried to add MyFirebaseMessagingService
into AndroidManifest.xml
like so:
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
There was some sort of a progress. The app crashed immediately after I sent a notification from Firebase console. Hence, I knew the app was aware of incoming notification but somehow crashed.
Below is my code to import and initialize a listener.
import messaging from '@react-native-firebase/messaging';
import { Alert } from 'react-native';
// Initialize notifications
const init = () => {
try {
messaging().onMessage((message) => {
Alert.alert('Received', JSON.stringify(message));
});
} catch (err) {
Alert.alert('Error', err.message);
}
};
In summary, I expect to receive a notification while the app is in foreground but nothing happens if I don't add MyFirebaseMessagingService
to AndroidManifest.xml
. If I add it, the app will crash on receiving notification.
回答1:
Okay. After further hair pulling and research, I found out that this was en expected behaviour rather than a bug.
Adding MyFirebaseMessagingService
or any other service to AndroidManifest.xml
to install react-native-firebase
v6 is unneccessary.
Afterwards, the app didn't receive any notification while in foreground but did receive them while in background.
Actually, it does. After peeking through Android log with adb logcat -s RNFirebaseMsgService:V
, I found out that react-native-firebase
v6 doesn't support notification as of now. This is their code which made me think my app didn't receive any notification while in foreground.
if (remoteMessage.getNotification() != null && remoteMessage.getData().size() == 0) {
// TODO broadcast intent when notifications module ready
return;
}
It's still a to-do item for them lmao!!
It turned out I need to send a notification with custom data because it already is supported. Hence, I need to use it every time so my app could display anything upon receiving such a notification.
来源:https://stackoverflow.com/questions/58533948/with-react-native-firebase-v6-app-crashed-when-receive-notification-while-in-fo