question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getInitialNotification is null if app is closed and opened from banner. Additional data in custom_notification

See original GitHub issue

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>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
Ashoatcommented, Mar 30, 2018

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.

0reactions
ishigamiicommented, Oct 4, 2018

@Ashoat Thanks a lot for this. it was just what I was missing 💯

Read more comments on GitHub >

github_iconTop Results From Across the Web

getInitialNotification() returns null if app opens after notification ...
Issue Notifications are received in Foreground/Background/Closed State. If I receive a notification while the app is closed and I then open the app...
Read more >
react-native-firebase. getInitialNotification gets null
But if my app is in background or app is killed, I receive notification but when I tap on notification, the value of...
Read more >
react-native-pushdy (RNPushdy) - npm
This method will return isAppOpenedFromPush = true if app opened from push (when app was killed). When app enters background (when opened from ......
Read more >
getInitialNotification - Notifee
The call returns a null value when the application wasn't launched by a notification. Once the initial notification has been consumed by this...
Read more >
Notifications | React Native Firebase
However, if the application is in the foreground, an event will be delivered ... details on how to add a custom notification property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found