react-native-fcm: getInitialNotification is null if app is closed and opened from banner. Additional data in custom_notification

What version of RN and react-native-fcm are you running? “react-native”: “0.46.4”, “react-native-fcm”: “^9.1.0”,

What device are you using? (e.g iOS9 emulator, Android 6 device)? android device nexus 5x

Is your app running in foreground, background or not running?

  1. Iam not able to achieve this behaviour: If app is closed (not running) and I receive notification, I want to show the notification and then be able to know, if the app was opened by clicking the banner, or app icon. I also expect to know if the banner was clicked when app is opened / in background, but that works fine (except the second question part below).

I send custom_notification payload to fcm according to docs, so after receiving silentNotification (with no notification payload) I let this package to build a local notification. But in both cases, whether I open the app by clicking the notification banner or app icon I get null value returned from FCM.getInitialNotification(). Does anybody know how to achieve the expected behaviour? I have spend too much time looking for answer here, but with no luck.

  1. Also I noticed that after clicking banner and receiving second notification with opened_from_tray value, I lost any additional data I send with originial notification, to make it really clear, I send:
{
  "to":"FCM_TOKEN",
  "data": {
    "custom_notification": {
      "body": "test body",
      "title": "test title",
      "color":"#00ACD4",
      "priority":"high",
      "icon":"ic_notification",
      "show_in_foreground": true
    },
   "someOtherData":"data", //this gets lost
   "someEvenMoreOtherData":"data2", //this gets lost
  }
}

after clicking the banner I receive:

body:"test body",
color:"#00ACD4",
fcm:{action: null},
icon:"ic_notification",
opened_from_tray:1,
priority:"high",
show_in_foreground:true,
title:"test title",

so just what is in custom_notification, so should I send my additional data also in custom_notification?

also my manifest file

<application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/SplashTheme">
      <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!-- FCM START-->
        <intent-filter>
            <action android:name="fcm.ACTION.HELLO" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <!-- FCM END-->

      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

      <!-- FCM START -->
        <service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service android:name="com.evollu.react.fcm.InstanceIdService" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
        <receiver android:enabled="true" android:exported="true"  android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
          <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>
</application>

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

I was having this issue as well. I am using react-native-splash-screen, which has its own launch activity: SplashActivity.

public class SplashActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
  }

}

The issue is that MessagingService is sending its Intent message to SplashActivity, which then starts MainActivity, and the message is lost. The solution is simple: just add the following line to SplashActivity after the intent is constructed:

intent.putExtras(this.getIntent());

This copies over the “extras” from MessagingService’s intent over to the new MainActivity intent, enabling FIRMessagingModule to later extract these extras and send them across the bridge to JavaScript.