Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

onNotification called only once in Android and not called again on notification open(userInteraction not updated) #333

Closed
Mindaugas-Jacionis opened this issue Jan 12, 2017 · 9 comments
Labels

Comments

@Mindaugas-Jacionis
Copy link

I have a function that is called on onNotification. This function checks for link that comes in data and checks for user interaction to be true - if condition passes then navigation function is called to navigate to needed screen in the app.

All this looks something like:

PushNotification.configure({
....
      onNotification: this._onNotification.bind(this),
....
});

_onNotification(notification) {
....
    if (notification && notification.userInteraction) {
      let data = notification.data || {};
      if (data && data.link) {
        //do navigation to link
      }
    }
  }

This works all good with iOS where notification.userInteraction changes from false to true on notification click and app opening. The onNotification is called once more and passes condition, therefore does the navigation. With android as far as I got from debugging the app - onNotification is called only once and only on notification being received, when you click on notification to open app - onNotification is not updated and called once more.

My current RN version is 0.39 and current react-native-push-notification version is 2.2.1

Any ideas or suggestions that would help to resolve the issue? :)

@GeoffreyPlitt
Copy link

I'm also seeing the app being opened when you click the notification, but onNotification not being called.

@edmondcang
Copy link

Me also. onNotification is called ONLY WHEN the app is running on foreground

@timothylui
Copy link

I get this too, any solution?

@Mindaugas-Jacionis
Copy link
Author

Mindaugas-Jacionis commented Feb 7, 2017

Maybe it is not nice to promote other package, but I didn't find easy fix so switched to https://github.com/wix/react-native-notifications/

It is easier to implement(less stuff to do in manifest) and it has separated methods: setNotificationReceivedListener and setNotificationOpenedListener, so it is easier to handle notifications and separate logics of these two states.

Main concern for me was that iOS and Android functionality is not unified, but I've simply created Notifications.android.js and Notifications.ios.js to keep it separate, but leave import to handle which one is needed.

@chromakode
Copy link

chromakode commented Mar 6, 2017

I am experiencing this as well. It appears that hasPoppedInitialNotification gets set to true on app start. Subsequently, when re-opening my app after notification interaction, I call configure() and the library does not check for an initial notification. I'm not sure what the expected behavior is -- I think I do not understand how this is supposed to be used, because it seems like the expectation here is that the JS environment for the app to be cleared out before it is possible to get another initial notification emitted in this way.

@elliotstokes
Copy link

Has there been any progress on this at all? I'm seeing the same. App open - onNotification called. App closed or in the background - Notification shown in notification center. Clicking on notification does not result in onNotification being called though.

@shrutic
Copy link

shrutic commented Jul 19, 2017

@npomfret @zo0r
Would appreciate your thoughts on this issue as this seems to exist for a long time

@shrutic
Copy link

shrutic commented Jul 20, 2017

Hope this helps others. Here is how I got the my Android code to read in data from notification when launching app from background/killed state.

there are two different notification logics for foreground and background/killed apps.

  • Foreground

onMessageReceived() is executed only for foreground apps. You can get “notification” and “data” by using remoteMessage.getNotification() and remoteMessage.getData().

  • Background/killed
    You can get “data” from your default launch Activity by using getIntent().getExtras(). Then parse “data” and do something.

Make sure your notification has 2 components in them
{
data: {
// Key value pairs that you need to read when app is launched
}
notificaiton: {
body: "" // text in the body of the notification UX,
title: "" // title of the notiifcation UX
}
}

Inside your MainActivity.java, do something along the lines of the following:

  @Override
            protected Bundle getLaunchOptions() {
              Intent mainIntent = getIntent();
                String dataValue = "";
                if (mainIntent != null) {
                    Bundle bundle = mainIntent.getExtras();
                    Log.v("RNPushNotification", "Data received in main: " + bundle);
                    if (bundle != null) {
                        JSONObject data = getPushData(bundle.getString("data"));
                        if (data != null && data.has("dataKey")) {
                            try {
                                dataValue = data.getString("dataKey");
                            } catch (Exception e) {
                                // no-op
                            }
                        }
                    }
                }
                initialProps.putString("dataValue", dataValue); // Read this inside your Root component in React native
                return initialProps;
            }

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

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

No branches or pull requests

7 participants