React-native-push-notification: onNotification is not getting called when opening the notification from background in Android

Created on 11 Aug 2020  路  19Comments  路  Source: zo0r/react-native-push-notification

onNotification is not getting called when opening the notification from a background in Android -- Remote Notifications with firebase.

Below are the files modified

build.gradle

buildscript {
ext {
    buildToolsVersion = "28.0.3"
    minSdkVersion = 16
    compileSdkVersion = 28
    targetSdkVersion = 28
    googlePlayServicesVersion = "+"
    firebaseVersion = "+"
}
repositories {
    google()
    jcenter()
}
dependencies {
    classpath("com.android.tools.build:gradle:3.5.2")
    classpath 'com.google.gms:google-services:4.3.3'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
 }
}

settings.gradle

include ':react-native-push-notification'
project(':react-native-push-notification').projectDir = file('../node_modules/react-native-push-notification/android')

app/build.gradle

implementation 'com.google.firebase:firebase-analytics:17.3.0'
implementation project(':react-native-push-notification')

--- end of the file added below line  ---
apply plugin: 'com.google.gms.google-services'

AndroidManifest.xml

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- Include this only if you are planning to use the microphone for video recording -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
    android:name="${applicationId}.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

 <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
            android:value="YOUR NOTIFICATION CHANNEL NAME"/>
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>

    <!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
                android:value="true"/>
    <!-- Change the value to false if you don't want the creation of the default channel -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.channel_create_default"
                android:value="true"/>
    <!-- Change the resource name to your App's accent color - or any other color you want -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

package.json

"react": "16.11.0",
"react-native": "0.62.2",
"react-native-push-notification": "^5.0.1",

pushNotificationService.js

  import React from 'react';
  import PushNotification from 'react-native-push-notification';

 export const configurePushNotification = () => {

 PushNotification.configure({

onRegister: function(token) {
  console.log('TOKEN:', token);
},


onNotification: function(notification) {
  console.log('NOTIFICATION:', notification);

},


onAction: function(notification) {
  console.log('ACTION:', notification.action);
  console.log('NOTIFICATION:', notification);

},

onRegistrationError: function(err) {
  console.error(err.message, err);
},

senderID: 'XXXXXXXXX',


permissions: {
  alert: true,
  badge: true,
  sound: true,
},


popInitialNotification: true,


requestPermissions: true,
   });
 };

App.js

import * as pushNotification from './src/services/pushNotificationService';

pushNotification.configurePushNotification();

const App = () => {
return (
    <AppNavigator />
  );
 };

Please, someone, help me out in this issue

Most helpful comment

 @Override
 public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
 }

Add this inside MainActivity.java

   public class SplashActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
   }
   }

This is SplashActivity.java

All 19 comments

Hi @vineelk8
I'm not able to reproduce your case. But I found and fix an issue with userInteraction on dev branch.
Are you sure onNotification is not called when the user press the notification?

This is happening with me too, @Dallas62, on Android, it only fires onNotification when the notification arrives, with userInteraction false, but not when pressed

@Dallas62 My team is experiencing a similar issue of onNotification not being called on Android, but only in one scenario. When a user backs out of the app using the physical android back button and then receives a push notification(the app is still running in the background), onNotification is not called. And when the user presses the PN, the app opens but onNotification is not called as well. Every other scenario works just fine(app is open in the foreground, user switches to a different app but our app is still in the background, user completely closes the app, etc) except this one scenario with the physical back button. Not sure if this related to this issue or if it would be better to open a separate issue, but figured I would make it known.

Hi @Dallas62 @PauloMello99 @TheNoodleMoose

The issue resolved for me, I'm using react-native-splash-screen so when I press notification from the background, the onNotification method is not triggered. So I fixed it on changing code in MainActivity.java and SplashScreen.Java

Hi @Dallas62 @PauloMello99 @TheNoodleMoose

The issue resolved for me, I'm using react-native-splash-screen so when I press notification from the background, the onNotification method is not triggered. So I fixed it on changing code in MainActivity.java and SplashScreen.Java

@vineelk8 What do you mean when you say 'changing code in MainActivity.java and SplashScreen.Java'? I recently implemented this lib and just faced this now

@PauloMello99

Did you use react-native-splash-screen npm package?

@vineelk8 Yep

 @Override
 public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
 }

Add this inside MainActivity.java

   public class SplashActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
   }
   }

This is SplashActivity.java

@vineelk8 It makes sense, you cannot get the initial extras if there's another activity being opened at first, thank you so much!!

@PauloMello99 Is your issue solved?

Hell yeah!

@PauloMello99 @vineelk8 thanks for the proposed solution. Looks like i'm getting the same issue. I don't see any SplashActivity.java file in react-native-splash-screen package though. Do I need to create one or ... how did you handle this ? Thanks

@PauloMello99 @vineelk8 thanks for the proposed solution. Looks like i'm getting the same issue. I don't see any SplashActivity.java file in react-native-splash-screen package though. Do I need to create one or ... how did you handle this ? Thanks

Please take a look in the react-native-splash-screen documentation, one of the steps to introduce the SplashScreenActivity is in the Android part

@PauloMello99 will do thanks. Someone else set it up at the project i'm working on, so I haven't seen that yet. Thanks 馃檶

 @Override
 public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
 }

Add this inside MainActivity.java

   public class SplashActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
   }
   }

This is SplashActivity.java

For anyone else struggling with this, the code snippet in MainActivity.java that @vineelk8 posted above was keeping my app from building and, upon further investigation, I didn't need it.

@TheNoodleMoose I'm was facing the same scenario, after some debugging I discover that we need to put PushNotification.popInitialNotification inside the App component in a useEffect/componentDidMount because when you press the back button and then open the app again react native doesn't restart all js code just the components, so putting popInitialNotification (prop or function) in a js outside the component scope will not run again when app start in this scenario.

But do not put .configure inside a component! just the PushNotification.popInitialNotification and pass popInitialNotification: false in configure.

I also made the above native code because I also have a SplashActivity.

 @Override
 public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
 }

Add this inside MainActivity.java

   public class SplashActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
   }
   }

This is SplashActivity.java

so many thanks for this solution, this solve my problem. it almost takes me a whole day on struggling all kinds of try and try....

 @Override
 public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
 }

Agregue esto dentro de MainActivity.java

   public class SplashActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
   }
   }

Esto es SplashActivity.java

Hi bro, Can you publish the MainActivity code and SplashActivity

I put de onNewIntent method in a MainActivity, but I don't see that the method called.

I feel that it's my problem.

My code

MainActivity.java
`................
import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen; // Import this.
import android.os.Bundle; // Import this.
import android.content.Intent;

public class MainActivity extends ReactActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this);
super.onCreate(savedInstanceState);
}
/**

  • Returns the name of the main component registered from JavaScript. This is used to schedule
  • rendering of the component.
    */
    @Override
    protected String getMainComponentName() {
    ......
    }

@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}

}`

My SplashActivity

`...
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
}

}`

 @Override
 public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
 }

Add this inside MainActivity.java

   public class SplashActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intent.putExtras(extras);
    }
    startActivity(intent);
    finish();
   }
   }

This is SplashActivity.java

For anyone else struggling with this, the code snippet in MainActivity.java that @vineelk8 posted above was keeping my app from building and, upon further investigation, I didn't need it.

This worked for me as well.

For me the onNotification never fired, it didn't matter if the push notification was in the background or foreground.
Either way, thanks for the help!

Was this page helpful?
0 / 5 - 0 ratings