calcazar / react-native-push-notification-CE

The community edition of the push notification library (by Zo0r) for applications made with React Native
MIT License
52 stars 23 forks source link

[Android] onNotification not called on click on notification #7

Closed avencat closed 6 years ago

avencat commented 6 years ago

Hi! First of all, thanks for maintaining this lib! I'm using it in my project but I have a weird issue on Android (I'm using FCM and I do get the onNotification called when I receive the notification). Indeed, when I tap on a notification, whether the app is opened or not, the onNotification method is not being called and if the app is already open, it entirely reloads... Do you have any workaround? I'm on development build and I'm only encountering this error on Android...

RN: 0.55.3 lib: master branch phone: Xiaomi Redmi Note 4X

calcazar commented 6 years ago

@avencat are you calling the appStart() function on your componentWillMount?

avencat commented 6 years ago

@calcazar yes, in the componentDidMount on the top level of my app! :)

avencat commented 6 years ago

Seems like it's a problem with FCM implementation: https://github.com/zo0r/react-native-push-notification/issues/697#issuecomment-391766566

calcazar commented 6 years ago

Looking at the comments.. what does your payload from FCM look like? Check this page out here:

https://github.com/calcazar/react-native-push-notification-CE/blob/master/trouble-shooting.md

Look over the silent, Mixed, and noisy notification. If your payload is structured as a noisy push, then tapping on the notification will trigger the onNotification call back

avencat commented 6 years ago

All right, I'll take a look on that on Monday, but fyi I have the notification in the Notification Center, the onNotification method is called when I receive the notification but not when I tap on the notification on the Notification Center...

avencat commented 6 years ago

@calcazar so the payload is as mixed (silent and noisy), I tried only noisy but still, the tap on the notification doesn't trigger the onNotification, the onNotification is only triggered when I receive a notification with the app running… :(

calcazar commented 6 years ago

@avencat that's bizarre.. do you think you can create a repo for me to take a look at it? I've tried what I can on my end to try and replicate the issue but haven't had any luck

avencat commented 6 years ago

@calcazar I can't create a repo right now but I'm still looking into this issue, I found a similar issue on another library, I'll try the solution out! https://github.com/invertase/react-native-firebase/issues/1027#issuecomment-390557446 The issue could be the SplashActivity according to this comment…

avencat commented 6 years ago

Ok so it seems this library (and others) are not compatible with some SplashScreen on Android as I removed my SplashScreen (on Android) and it now works as expected! To remove the Adnroid SplashScreen I reversely followed this tutorial : https://medium.com/handlebar-labs/how-to-add-a-splash-screen-to-a-react-native-app-ios-and-android-30a3cec835ae (except the react-native-splash-screen parts as I didn't have this library) Thanks for your time @calcazar and if you have any idea on how to implement a SplashScreen on Android compatible with this library, please tell me! :D

nSimonFR commented 6 years ago

I too have the same problem but I don't think it is related to SplashScreen as I do not have one configured in my Android Project...

sergey-kozel commented 5 years ago

Indeed, this can happen due to splash activity and it is a quite common issue on Android. And it's quite easy to fix that. When you tap on your push notification the system sent new intent with notification payload to your activity and under the hood this library hooks that intent and retrieves data. But when you have the splash activity the intent is sent to it, not to your main activity. All that you need to do is forwarding the extras further:


public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        Intent intent = new Intent(this, MainActivity.class);  
        if (getIntent().getExtras() != null) {
            intent.putExtras(getIntent().getExtras());
        }
        startActivity(intent);
        finish();
    }
}
mikehardy commented 5 years ago

There are similar issues with splash screen and firebase, if your module makes assumptions about being a single Activity, it's actually easy to integrate splash screen in a single activity - working code here https://github.com/crazycodeboy/react-native-splash-screen/issues/289

TaraSinghDanu commented 3 years ago

Indeed, this can happen due to splash activity and it is a quite common issue on Android. And it's quite easy to fix that. When you tap on your push notification the system sent new intent with notification payload to your activity and under the hood this library hooks that intent and retrieves data. But when you have the splash activity the intent is sent to it, not to your main activity. All that you need to do is forwarding the extras further:

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

Thank you very much @sergey-kozel ,This works for me.

Ajmal0197 commented 3 years ago

The issue is due to splash activity Edit the manifest like this and it gonna work:

    <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>
  <activity
    android:name=".MainActivity"
    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.INFO" />
      </intent-filter>
    </activity>
mikehardy commented 3 years ago

Everyone should use react-native-bootsplash at this point. react-native-splash-screen is buggy and unmaintained

https://github.com/zoontek/react-native-bootsplash