OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 373 forks source link

Event not fire #522

Closed dmitryshelomanov closed 6 years ago

dmitryshelomanov commented 6 years ago

code

    subscribe = () => {
      OneSignal.addEventListener('received', this.onReceived)
      OneSignal.addEventListener('opened', this.onOpened)
      OneSignal.addEventListener('ids', this.onIds)
      OneSignal.configure()
    }

rn 0.55 onesignal 3.2.3 android 8 on notify open not fire event. Received not fire too

dmitryshelomanov commented 6 years ago

and periodically fall down

dmitryshelomanov commented 6 years ago

Full code

export function OneSignalProvider(NavigatorInstance) {
  class withOneSignal extends Component {
    static router = NavigatorInstance.router

    componentWillReceiveProps(nextProps) {
      if (this.props.token !== nextProps.token && nextProps.token) {
        this.subscribe()
      }
    }

    incrementCounter = () => {
      this.props.incrementCount()
    }

    onReceived = (data) => {
      console.log('received')
      this.handleCommand('recived', data)
    }

    onOpened = ({ notification }) => {
      console.log('onOpened')
      this.handleCommand('opened', notification)
    }

    onIds = (data) => {
      this.props.setPlayerId(data)
    }

    handleCommand(type, data) {
      const command = data.payload.additionalData && data.payload.additionalData.command

      if (typeof commands[command] !== 'undefined') {
        commands[command][type].call(this, data.payload.additionalData)
      }
    }

    componentWillUnmount() {
      this.unsubscribe()
    }

    componentWillMount() {
      OneSignal.init(onesignalAppId)
      OneSignal.inFocusDisplaying(0)
    }

    subscribe = () => {
      OneSignal.addEventListener('received', this.onReceived)
      OneSignal.addEventListener('opened', this.onOpened)
      OneSignal.addEventListener('ids', this.onIds)
      OneSignal.configure({})
    }

    unsubscribe = () => {
      OneSignal.removeEventListener('received', this.onReceived)
      OneSignal.removeEventListener('opened', this.onOpened)
      OneSignal.removeEventListener('ids', this.onIds)
    }

    render() {
      return (
        <NavigatorInstance />
      )
    }
  }

  return enhance(withOneSignal)
}

Adb logcat empty

Nightsd01 commented 6 years ago

@userbq201 We usually suggest adding the event listeners before OneSignal.init() gets called, so I would suggest moving the addEventListener(...) calls to componentWillMount(), just above init().

Also, the logcat is empty because you are not using setLogLevel.

Please add OneSignal.setLogLevel(6, 0) above OneSignal.init(...) and let me know if you see any errors printed to console?

a-darul commented 6 years ago

+1 same issue

neo125874 commented 6 years ago

@Nightsd01 hi, i found that if remove the OneSignal.configure({}); the event would fire, but the onIds(device) {} would not be triggered,

any solution about this?

dmitryshelomanov commented 6 years ago

@Nightsd01 no error (add loger above init)

dmitryshelomanov commented 6 years ago

ids was called. Where app in background notification work. But event not sunscribe

dmitryshelomanov commented 6 years ago

@Nightsd01

      OneSignal.addEventListener('received', this.onReceived)
      OneSignal.addEventListener('opened', this.onOpened)
      OneSignal.addEventListener('ids', this.onIds)
      // OneSignal.configure({})
      OneSignal.init(onesignalAppId)
      OneSignal.inFocusDisplaying(0)

not called

      OneSignal.addEventListener('received', this.onReceived)
      OneSignal.addEventListener('opened', this.onOpened)
      OneSignal.addEventListener('ids', this.onIds)
      OneSignal.configure({})
      OneSignal.init(onesignalAppId)
      OneSignal.inFocusDisplaying(0)

not called

dmitryshelomanov commented 6 years ago

In docs

componentWillMount() {
    OneSignal.init("YOUR_ONESIGNAL_APPID");

    OneSignal.addEventListener('received', this.onReceived);
    OneSignal.addEventListener('opened', this.onOpened);
}
dmitryshelomanov commented 6 years ago

In versiot 3.1.4 key was be in build.gradle

        manifestPlaceholders = [
            onesignal_app_id:
            onesignal_google_project_number
]

Now

Onesignal.init(onesignal_app_id)
Nightsd01 commented 6 years ago

@userbq201 What does "periodically fall down" mean? I am having a lot of trouble understanding what you are saying...?

Can you reproduce this issue in our example project? It is very difficult to solve this issue since you are not posting details, ie. your other react-native dependencies, your app/build.gradle, so on.

dmitryshelomanov commented 6 years ago

minsdk 17 targetsdk 27 compilesdk 27

dmitryshelomanov commented 6 years ago

dependency react native map react native input mask react native splash screen react native geocoder

No errors. Event register called in native module (I modify your js file in lib)

dmitryshelomanov commented 6 years ago

@Nightsd01 manifestPlaceholders not used ?

pearlismylove commented 6 years ago

+1 Same here. ids triggered. but received and opened never triggered

My dependencies

"dependencies": { "axios": "^0.18.0", "create-react-class": "^15.6.3", "react": "16.3.1", "react-native": "0.55.4", "react-native-animatable": "^1.2.4", "react-native-google-signin": "^0.12.0", "react-native-keyboard-aware-scroll-view": "^0.5.0", "react-native-onesignal": "^3.2.4", "react-native-shimmer": "^0.3.2", "react-native-vector-icons": "^4.6.0", "react-navigation": "^2.0.1", "react-redux": "^5.0.7", "redux": "^4.0.0", "redux-logger": "^3.0.6", "redux-thunk": "^2.2.0", "underscore": "^1.9.0" },

pearlismylove commented 6 years ago

received and opened both are triggered in release mode (react-native run-android --variant=release)

but not triggered at all in debug mode (react-native run-android)

naturom commented 6 years ago

I have the same issue as well... onIds, onReceive, onOpened all not triggered

dmitryshelomanov commented 6 years ago

@naturom in release triggered

naturom commented 6 years ago

@userbq201 yea, but hard to debug, hope it fix soon

Nightsd01 commented 6 years ago

If anyone is still experiencing this issue, please post a logcat of the first 10 seconds of running your app.

ayushnawani commented 6 years ago

same issue as @pearlismylove mentioned, events not getting triggered in debug mode but only in android

julioapm commented 6 years ago

Same here. I'm using the oficial sample and when debugging the onIds() event will never fire. So it's not related to any other dependency beyound those found in the sample.

Nightsd01 commented 6 years ago

@ayushnawani please post a logcat with OneSignal.setLogLevel(6, 0)....

@julioapm Is it just onIds that doesn't fire? Please be aware that onIds will only fire when the subscription state (ID's) change. Otherwise, the event will not fire. If you want to force it to fire, you can call OneSignal.configure(). It's confusing, but it's legacy code, I'm planning to refactor to make this a bit more obvious in the next major release.

ayushnawani commented 6 years ago

@Nightsd01 @userbq201 don't know how but now it's working for me, sometimes its just taking 40-50 seconds to trigger onReceived event but its working.

Nightsd01 commented 6 years ago

@ayushnawani Are you saying it is taking 40-50 seconds AFTER the device shows the notification? Or is it taking 40-50 seconds for the device to receive the notification from OneSignal?

Nightsd01 commented 6 years ago

In any case since no one has responded with a logcat I'll assume that they've been able to fix this issue in their projects, so I'll be closing.

If anyone encounters this issue going forward, please post on this issue (with OneSignal.setLogLevel(6, 0) and a logcat covering at least the first 30 seconds after launching your app) and I'll be happy to reopen the issue and investigate

ayushnawani commented 6 years ago

@Nightsd01 sometimes after 40-50 seconds onReceived(notification) is getting called for me.

onReceived(notification) {
    console.log('Notification received: ', notification);
}
dmitryshelomanov commented 6 years ago

@Nightsd01 logcat empty. Notification not show after 40-50s

julioapm commented 6 years ago

@Nightsd01 the logcat doesn't show any erros. Thanks for the info on the semantics of onIds(). I'm using now .getPermissionSubscriptionState() in order to get the registration ID and it's working. As you suggested, onIds() remains in case the IDs change (could not test it though).

Thanks.

erenalgan commented 6 years ago

@userbq201 If this is still failing, or failing for some other people, I found out (in my case) what was going on. Since I didn't register eventhandlers at the beginning of App initialization (e.gindex.js), removing OneSignal.removeEventListener functions from my component fixed the issue. Basically componentWillUnmount was being called before the events fire, hence events failed.

ddereszewski commented 5 years ago

Logcat from Android 8 after receiving a push message.

2018-10-11 15:47:01.524 7238-7496/com.levuro.engage2 D/OneSignal: OneSignalRestClient: Making request to: https://onesignal.com/api/v1/notifications/4bc49124-1064-437b-b57a-ee633c5c9806
2018-10-11 15:47:01.525 7238-7324/com.levuro.engage2 D/EGL_emulation: eglMakeCurrent: 0x9767e100: ver 3 0 (tinfo 0x86c43930)
2018-10-11 15:47:01.525 7238-7496/com.levuro.engage2 D/OneSignal: OneSignalRestClient: PUT SEND JSON: {"app_id":"03da811f-3c6d-4598-98ae-6ea830006bb5","player_id":"55b94c2c-af32-4943-8463-9553f553a54d","opened":true}
2018-10-11 15:47:01.581 7238-7324/com.levuro.engage2 D/EGL_emulation: eglMakeCurrent: 0x9767e100: ver 3 0 (tinfo 0x86c43930)
2018-10-11 15:47:01.656 7238-7496/com.levuro.engage2 V/OneSignal: OneSignalRestClient: After con.getResponseCode  to: https://onesignal.com/api/v1/notifications/4bc49124-1064-437b-b57a-ee633c5c9806
2018-10-11 15:47:01.656 7238-7496/com.levuro.engage2 D/OneSignal: OneSignalRestClient: Successfully finished request to: https://onesignal.com/api/v1/notifications/4bc49124-1064-437b-b57a-ee633c5c9806
2018-10-11 15:47:01.670 7238-7496/com.levuro.engage2 D/OneSignal: PUT RECEIVED JSON: {"success":true}