1

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.

Dorklord
  • 342
  • 6
  • 11

1 Answers1

3

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.

Dorklord
  • 342
  • 6
  • 11
  • 1
    Dude, I had been struggling with same issue for 2 days before I found your answer. You are a godsend for me, thank you very much! If someone want to test "data type" message delivering you could use curl/postman to send requests: https://stackoverflow.com/a/37376757/3175823 – fotonmoton Mar 11 '20 at 15:24
  • @fotonmoton glad I could help. Rock on!! – Dorklord Mar 12 '20 at 18:48