React-native-push-notification: onNotification not called when clicking on GCM notification while app is in the background

Created on 19 Mar 2018  Â·  12Comments  Â·  Source: zo0r/react-native-push-notification

I'm on version 3.0.2

When I send a GCM notification via node-pushnotification, I receive the notification on my android simulator.

When I click the notification (app is running in the background), it opens the app but does not call onNotification. I've tried with both popInitialNotification true and false, no dice.

I've gone through #652 #495 #490 #72, and several others in my googling; but not found why this isn't working. My code below includes several suggestions from these other threads.

I've set up a separate pushNotification.js file that gets included in index.js to make sure it gets set up immediately, then my component consumes via the default export:

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

let queued = [];
let config = null;

const queue = (fn) => (...args) => {
  if (!config) {
    queued.push([fn, args])
  }
  else {
    fn(...args);
  }
};

const releaseQueue = () => {
  queued.forEach(queueItem => queueItem[0](...queueItem[1]));
  queued = [];
};

PushNotification.configure({
  onNotification: queue((notification) => {
    config.onNotification(notification)
  }),
  onRegister: queue((...args) => {
    config.onRegister(...args)
  }),
  senderID: 'mySenderId',
  popInitialNotification: true
});

export default (c) => {
  config = c;
  releaseQueue()
};

I tried setting up an intent filter like in some of the comments that say their problems were solved, so that maybe I could at least have the push info in my initial props. No dice:

    <intent-filter>
      <action android:name="OPEN_MAIN_ACTIVITY" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
    return new ReactActivityDelegate(this, getMainComponentName()) {
        private JSONObject getPushData(String dataString) {
            try {
                return new JSONObject(dataString);
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        protected Bundle getLaunchOptions() {
            Intent mainIntent = getIntent();
            String dataValue = "";
            Bundle initialProps = new Bundle();
            if (mainIntent != null) {
                Bundle bundle = mainIntent.getExtras();

                if (bundle != null) {
                    JSONObject data = getPushData(bundle.getString("data"));
                    if (data != null) {
                        try {
                            dataValue = data.toString();
                        } catch (Exception e) {
                            // no-op
                        }
                    } else {
                    }
                }
            }
            initialProps.putString("pushData", dataValue); // Read this inside your Root component in React native
            return initialProps;
        }
    };
}

My server side code:

const PushNotifications = new require('node-pushnotifications');

const push = new PushNotifications({
  gcm: {
    id: 'my id'
  }
})

  await push.send([token], {
    title: 'My Title',
    body: 'body',
    topic: 'com.myapp',
    custom: {
      myCustomThing: 'myCustomThing
    },
    clickAction: "OPEN_MAIN_ACTIVITY",
  });

Since this wasn't able to get answered on StackOverflow, I'm guessing this must be some kind of bug with the library.

Interestingly, local notifications _do_ trigger onNotification, even when the app is in the background. So maybe this has something to do with my server code? Or with GCM specifically?

Stale

Most helpful comment

any update?

All 12 comments

From what I've seen the onNotification callback is triggered only when you run a Release variant...

I tried running it with react-native run-android --variant=release

Does it just not work on simulators?

It worked with a emulator for me.. How did you do the check? You can show a toast from the callback to see if it is called.

onNotification works and prints notification object, but localNotifications but won't display the notification on my older physical ipad device. Any suggestions.

@hedaktibo I was checking using a console log inside the on notification, then watching the android logs.

Well.. you can't really debug a release variant, that's why I suggested to show a toast (ToastAndroid) with the notification data from the callback, not a console.log.

Just did a toast. Doesn't trigger when a notification comes in while app is in the background, and you click on that notification. It does get triggered when the app is open and you get a notification, though.

Just tried it on a real android phone using react-native run-android --variant=release. Still no toast.

I'm receiving the notification, but it doesn't pop-up. This is not useful
on IOS since there's no indication that a notification has been received
which is at least present on android.

On Thu, Mar 22, 2018, 3:17 PM Noah Prince notifications@github.com wrote:

Just tried it on a real android phone using react-native run-android
--variant=release. Still no toast.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/zo0r/react-native-push-notification/issues/681#issuecomment-375426385,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AXnGLp5tBn90w4gp0nbiw2PGpuR8gAfEks5tg_jBgaJpZM4Swgla
.

any update?

React Native Push Notifications clearly states that we need to call configure outside of the React Native Lifecycle. This is very important.

https://product.farewell.io/visible-react-native-push-notifications-that-work-on-both-ios-and-android-5e90badb4a0f

https://prnt.sc/k61hz7

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cidevant picture cidevant  Â·  3Comments

selimonline picture selimonline  Â·  3Comments

anativ picture anativ  Â·  3Comments

sungjinoh picture sungjinoh  Â·  3Comments

DaniShalash picture DaniShalash  Â·  3Comments