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.

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

See original GitHub issue
"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.

Issue Analytics

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

github_iconTop GitHub Comments

15reactions
anubhav193commented, 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);

3reactions
nonameolssoncommented, Sep 7, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Clicking on notification doesn't open Android app
The code which I am using for opening app on the click of notification, which is working perfectly fine :
Read more >
Notifications Not Shown - Mobile Push
Android Settings App > Notifications > Your App you should see "Show notifications" enable and all categories underneath enabled. If you have a...
Read more >
Create a Notification - Android Developers
To get started, you need to set the notification's content and channel ... in the background so the action does not interrupt the...
Read more >
Receive messages in an Android app - Firebase - Google
Any notification message that does not explicitly set the icon in the notification payload. Android uses the custom default color for. All notification...
Read more >
How to Set up Push Notifications in React Native - Around25
When you get on the firebase site, click on the Android icon to create the ... Since the app will not show notification...
Read more >

github_iconTop Related Medium Post

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