Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking on local notification - onNotification doesn't get called when app is not started (Android) #516

Closed
andrei-m-code opened this issue Aug 10, 2017 · 5 comments

Comments

@andrei-m-code
Copy link

andrei-m-code commented Aug 10, 2017

"react": "16.0.0-alpha.12",
"react-native": "^0.45.1",
"react-native-push-notification": "^3.0.0"

On application start I have this code:

PushNotification.configure({
    onNotification: function(notification) {
        console.log('onNotification');
        ToastAndroid.show('onNotification', 3000);
    }
});

My background service sends local push notification:

PushNotification.localNotification({
    message: 'Hello World',
    smallIcon: 'ic_launcher'
});

The notification gets delivered. When I click it, onNotification method doesn't get called, then when I receive another notification, it actually gets called. It seems like it works only if app is in memory atm.

Am I doing something wrong?

stackoverflow question here.

@andrei-m-code andrei-m-code changed the title onNotification doesn't get called (Android) onNotification doesn't get called when app is not started (Android) Aug 10, 2017
@andrei-m-code andrei-m-code changed the title onNotification doesn't get called when app is not started (Android) Clicking on local notification - onNotification doesn't get called when app is not started (Android) Aug 14, 2017
@andrei-m-code
Copy link
Author

andrei-m-code commented Aug 17, 2017

In my code I configured notifications outside App class - that was an issue. I just moved the configuration to App constructor and now it works just fine, onNotification gets called with no problem:

class App extends React.Component {
    constructor(props) {
        super(props);
        PushNotification.configure({ ... });
    }
}

@nonameolsson
Copy link

@Nonsense66 Hi! I can't get it to work.. This is how I have it.

App.js

class App extends Component {
  constructor () {
    super()

    PushConfig.configure(store.dispatch)
  }

PushConfig.js

import PushNotification from 'react-native-push-notification'
import NotificationActions from '../Redux/NotificationRedux'
import PNHelper from '../Lib/PushNotificationHelpers'

export default {
  // dispatch is passed in from App.js after creating store
  configure: dispatch => {
    // https://github.com/zo0r/react-native-push-notification
    PushNotification.configure({
      // (optional) Called when Token is generated (iOS and Android)
      onRegister: token => {
        console.tron.display({ name: 'Notification register', preview: token })
      },

      // (required) Called when a remote or local notification is opened or received
      onNotification: notification => {
        console.tron.display({
          name: 'Notification',
          preview: 'Notification clicked',
          value: { notification }
        })
        // PNHelper.cancelReminder(
        //   'abc123',
        //   NotificationActions.removeInspectionReminder('abc123')
        // )
        // dispatch(NotificationActions.removeInspectionReminder(notification.message))
      },

      // ANDROID ONLY: (optional) GCM Sender ID.
      senderID: 'YOUR GCM SENDER ID',

      // IOS ONLY (optional): default: all - Permissions to register.
      permissions: {
        alert: true,
        badge: true,
        sound: true
      },

      // Should the initial notification be popped automatically
      // default: true
      // Leave this off unless you have good reason.
      popInitialNotification: false,

      /**
      * IOS ONLY: (optional) default: true
      * - Specified if permissions will requested or not,
      * - if not, you must call PushNotificationsHandler.requestPermissions() later
      * This example app shows how to best call requestPermissions() later.
      */
      requestPermissions: true
    })
  }
}

Nothing is different, the onNotification is nevered triggered without pressing/clicking the notification.

@AugustoAleGon
Copy link

I have the same problem!

@anubhav193
Copy link

anubhav193 commented Nov 6, 2017

For people who still haven't figured this out, here is what solved it in my case:

In my reducers.js file

import configurePushNotification from './pushController'

export default function configureStore() {
	var store = createStore(allReducers, applyMiddleware(
		thunkMiddleware
	))
	configurePushNotification(store.dispatch, store.getState)
	return store
}

In my pushController.js file

import PushNotification from 'react-native-push-notification';
import {
	AppState
} from 'react-native'

const onNotification = (notification) => {
    if (notification.userInteraction) { console.log('User clicked notification') }
    else { console.log('User received notification') }
}

export default (dispatch, getState) => {
	PushNotification.configure({
		onRegister: function(token) {
			console.log('Generate push token:', token);
		},
		onNotification: onNotification,
		senderID: XXXXXX,
	});
}

// The magic happens here
const appStateListener = (state) => {
	if (state === 'active') {
		PushNotification.popInitialNotification(notification => {
			if (notification) {
				onNotification(notification)
			}
		});
	}
};

AppState.addEventListener('change', appStateListener);

@nonameolsson
Copy link

@anubhav193 that should be in the documentation, thanks!

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

No branches or pull requests

4 participants