4

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

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 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've gone through every single thread of every issue on react-push-notification relating to this issue. I even tried setting up an intent filter like so, 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;
        }
    };
}

Sort of insane how difficult it is to get this thing to work.

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",
  });
Noah
  • 1,608
  • 15
  • 31

1 Answers1

0

Maybe you need to add this line to androidManifest:

android:launchMode="singleInstance"
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
haibtbk
  • 11
  • 1