Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onNotification is not getting called when opening the notification from background in Android #1592

Closed
vineelk8 opened this issue Aug 11, 2020 · 26 comments

Comments

@vineelk8
Copy link

vineelk8 commented Aug 11, 2020

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

@Dallas62
Copy link
Collaborator

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?

@PauloMello99
Copy link

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

@TheNoodleMoose
Copy link

@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.

@vineelk8
Copy link
Author

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

@PauloMello99
Copy link

PauloMello99 commented Aug 18, 2020

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

@vineelk8
Copy link
Author

@PauloMello99

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

@PauloMello99
Copy link

@vineelk8 Yep

@vineelk8
Copy link
Author

vineelk8 commented Aug 18, 2020

 @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

@PauloMello99
Copy link

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

@vineelk8
Copy link
Author

vineelk8 commented Aug 18, 2020

@PauloMello99 Is your issue solved?

@PauloMello99
Copy link

Hell yeah!

@marudy
Copy link

marudy commented Aug 23, 2020

@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
Copy link

@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

@marudy
Copy link

marudy commented Aug 24, 2020

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

@ehubbell
Copy link

 @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.

@danieloprado
Copy link

danieloprado commented Sep 22, 2020

@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.

@cengit
Copy link

cengit commented Nov 16, 2020

 @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....

@alejogo4
Copy link

alejogo4 commented Nov 27, 2020

 @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();
}

}`

@ionTea
Copy link

ionTea commented Mar 3, 2021

 @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!

@icancodealright
Copy link

@vineelk8 if I was using splash screen and with your solution, my notification() is working, but can you please explain why it was not working without the changes?

@Dallas62
Copy link
Collaborator

Dallas62 commented Jul 10, 2021

@vineelk8 if I was using splash screen and with your solution, my notification() is working, but can you please explain why it was not working without the changes?

Hi,
Because the splashscreen activity intercept all information and don't pass it to the main activity (your react application).
Please refer to the Android concept of Activity.
Regards

@nachoperez714
Copy link

Can you please add this common SplashActivity error to the README in a kind of "Common errors" section? It seems like it happened to a lot of people and it would stop people from creating the same issue over and over again

@Dallas62
Copy link
Collaborator

Hi @nachoperez714
An information is already in the README (Installation steps).
Feel free to make a PR.
Regards.

@UmerQur3shi
Copy link

@Dallas62 @vineelk8 I'm facing the same issue, onNotification is not triggered when my app is in background and I click the notification pop-up. I couldn't find SplashActivity.java anywhere in the project. Please help.

@murilokrugner
Copy link

I'm also having the same problem

@UmerQur3shi
Copy link

I'm also having the same problem

Use notifee instead of react native push notification, this library is not being maintained for a long time now. I have tested notifee for the task and it worked like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests