question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onNotification not called when clicking on GCM notification while app is in the background

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12

github_iconTop GitHub Comments

7reactions
iamrutvikcommented, Jul 13, 2018

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

7reactions
roysGcommented, Mar 30, 2018

any update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

onNotification() is not fired when app is killed - Stack Overflow
I am not positive but when I remember correctly killing (alias force closing) an app ... GCM push notification works after app Force...
Read more >
Receive messages in an Android app - Firebase - Google
Notification messages delivered when your app is in the background. ... may be shorter depending on OS delays incurred ahead of calling onMessageReceived...
Read more >
Push notification in Background wo… | Apple Developer Forums
While the app is in background and the app received push notification, ... in Background, that function is called in Active but not...
Read more >
SDK Notification Event Handlers - OneSignal Documentation
Must have background mode enabled and send the push with content_available in the iOS Message Settings for method to be called while app...
Read more >
Handling Incoming Push Notifications in AWS Amplify - Medium
It will also not trigger if a push notification is received while the app is in the background or closed, but the user...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found