0

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;  
        }
      })
Mariam
  • 3
  • 1
  • 4
  • Welcome to Stack Overflow! Could you please provide more details to help us answer your question? – jkdev Jul 06 '18 at 11:02
  • OK. If you click the "edit" link, you can edit your question and include those details. – jkdev Jul 06 '18 at 11:18

2 Answers2

3

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>

mosabbir tuhin
  • 560
  • 4
  • 14
  • Hey, I have some issue like this can you check this Q please?https://stackoverflow.com/questions/57171787/navigate-to-screen-after-opening-a-notification @mosabbirtuhin – DevAS Jul 23 '19 at 21:22
0

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(() => {

  });
Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90