0

I was getting some issues with react-native-fcm and react-native-push-notification, so I decided to try react-native-firebase.

I am able to receive the following payload sent by the server when the app is in foreground:

{
           "to":"FCM_TOKEN",
            "data": {
                "icon": "http://host.com/img.jpg",
                "title": "xyz title",
                "body": "sadsaddsasadsda",
                "click_action": "https://host.com/id/391",
                "data": "some json.stringify custom data"
            }

}

However, when the app is in background, FCM.onMessage() isn't receiving any notification at all. I could receive those data in the background with react-native-fcm or react-native-push-notification, so I wonder if this is the limitation of react-native-firebase.

wixRoutes.js

import firebase from 'react-native-firebase';
import {Navigation} from 'react-native-navigation';

let FCM = firebase.messaging();

FCM.requestPermissions();

FCM.getToken().then(token=>{
    console.log('has token',token);
});

FCM.onTokenRefresh((refreshedToken) => {
    console.log(refreshedToken);
});

FCM.onMessage((message=>{
    console.log('new message',message);
}));

FCM.getInitialNotification().then(notif => {
    console.log('initial message',notif);
});


function startApp() {
   Navigation.startSingleScreenApp(/..../);
}

startApp();

AndroidManifest.xml

<!-- FCM -->
<service
        android:name="io.invertase.firebase.messaging.MessagingService"
        android:enabled="true"
        android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.InstanceIdService" android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

<!-- If you would like to schedule local notifications then you also need to add the following:-->
<receiver android:name="io.invertase.firebase.messaging.RNFirebaseLocalMessagingPublisher"/>
<receiver android:enabled="true" android:exported="true" android:name="io.invertase.firebase.messaging.RNFirebaseSystemBootEventReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
<activity android:launchMode="singleTop" /......./ />
RedGiant
  • 4,444
  • 11
  • 59
  • 146

1 Answers1

0

The reason was that my phone Xiaomi Red Note 4X has this option "Don't keep activities" as default in the developer option, which kills the app once it's in the background, making it unable to receive any notifications. Dsable that option and it works fine.

RedGiant
  • 4,444
  • 11
  • 59
  • 146