Mariam
Mariam

Reputation: 3

react-native-firebase. getInitialNotification gets null

When click on noification from the tray getInitialNotification is triggered but notificationOpen param gets null value.

I am sending notification via firebase console. If my app is in foreground I receive my notification data that I am sending. But if my app is in background or app is killed, I receive notification but when I tap on notification, the value of openNotification is null.

This.is what I am doing.

firebase.notifications().getInitialNotification()
      .then((notificationOpen: NotificationOpen) => {
        console.log('Notification closed')
        console.log(notificationOpen)
        if (notificationOpen) {
          // App was opened by a notification
          // Get the action triggered by the notification being opened
          const action = notificationOpen.action;
          // Get information about the notification that was opened
          const notification: Notification = notificationOpen.notification;  
        }
      })

Upvotes: 0

Views: 3633

Answers (2)

mosabbir tuhin
mosabbir tuhin

Reputation: 625

create SplashActivity.java and below code

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MainActivity.class);

        // this line is necessary to open notification when app is closed
        intent.putExtras(this.getIntent());
        startActivity(intent);
        finish();
    }
}

add splash activity to AndroidManifest.xml

...
<activity
        android:name=".SplashActivity"
        android:theme="@style/SplashTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
...

add splash resource to Styles.xml

...
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
       <item name="android:windowBackground">@drawable/background_splash</item>
       <item name="android:statusBarColor">@color/darkblue</item>
</style>
...

create background_splash.xml and create resource to display when app launch

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/blue"/>

    <item android:gravity="center">
        <bitmap
            android:tileMode="disabled"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />
    </item>

</layer-list>

Upvotes: 3

Jude Fernandes
Jude Fernandes

Reputation: 7517

You should implement the getInitialNotification and notificationOpenedListener in your app, depending on the state of the app either one of them is called when you tap on a notification but you can be sure that only 1 of them will be called.

firebase
  .notifications()
  .getInitialNotification()
  .then(notificationOpen => {
    if (notificationOpen) {

    }
  });



this.notificationOpenedListener = firebase
  .notifications()
  .onNotificationOpened(() => {

  });

Upvotes: 0

Related Questions