React-native-push-notification: Action, onNotification not working with localNotification

Created on 12 Dec 2019  路  6Comments  路  Source: zo0r/react-native-push-notification

Hi and thanks for your work

I can't get Action working, and even the onNotification parameter notify nothing.

Here is the code i put in my App.js componentDidMount
there is the configuration and the action handler

 componentDidMount() {
    PushNotification.configure({
      // (required) Called when a remote or local notification is opened or received
      onNotification: function(notification) {
        console.log('NOTIFICATION:', notification);
      },
      popInitialNotification: true,
      requestPermissions: true,
    });

    (function() {
      PushNotificationAndroid.registerNotificationActions(['Lire']);
      DeviceEventEmitter.addListener('notificationActionReceived', function(
        action,
      ) {
        console.log('Notification action received: ' + action);
        const info = JSON.parse(action.dataJSON);
        if (info.action === 'Lire') {
          console.log('do the action');
        }
        // Add all the required actions handlers
      });
    })();
  }

here is my function that i call to receive notification

const registerLocalNotification = article => {
  const titre = he.decode(article.titre); 
  PushNotification.setApplicationIconBadgeNumber(0);
  PushNotification.localNotification({
    /* Android Only Properties */
    vibrate: true,
    vibration: 300,
    priority: 'high',
    visibility: 'public',
    importance: 'high',

    /* iOS and Android properties */
    title: 'nouvel article',
    message: titre, // (required)
    playSound: false,
    number: 1,
    actions: '["Lire"]',
  });
};

The notifications work fine but:

  • when I click on the action button (on the notification message). Nothing is happening. I can't see the console.log() in the DeviceEventEmitter.addListener method
  • i can't even see the console.log('NOTIFICATION:', notification); in the PushNotification.configure method

My configuration is:
react-native-cli: 2.0.1
react-native: 0.61.4
debian 10
and i test on emulated device with Android 9

Can someone tell me what do i miss ?

Most helpful comment

Finally after changing some code directions. I made it work so sharing the code and have a look on it might helpful to you :)

`var PushNotification = require("react-native-push-notification");

export class Participant extends React.Component {

componentWillMount() {
    PushNotification.configure({
        onNotification: (notification) => {

            //Code to print array into console
            console.log(notification);

            //Code to be executed according to the action selected by the user
            if (notification.action == "Copy ID") {
                Clipboard.setString('hello id');
            }
            else if (notification.action == "Copy Password") {
                Clipboard.setString('hello password');
            }
        },
        popInitialNotification: true,
        requestPermissions: true,
    });
}

//Function to generate count down timer
generateCountdownTimer = (index, eventDate, eventTime) => {

            return <CountDown style={{ marginBottom: 5, }} until={eventTimeIntoSeconds} size={17}
                onFinish={() => {

                    //Code to display push notification to the user into status bar
                    PushNotification.localNotification({
                        id: index,
                        largeIcon: "ic_launcher",
                        title: "1 new notification",
                        message: //Your msg here
                        vibrate: true,
                        vibration: 300,
                        priority: "high",
                        actions: '["Copy ID", "Copy Password"]',

                    });
                }
                } />
        }

}`

All 6 comments

+1
Might I add that the notification is not dismissed when an action is clicked. It just hangs there.

+1
Getting the same thing here on RN: 0.61.4 and 3.1.9

Finally after changing some code directions. I made it work so sharing the code and have a look on it might helpful to you :)

`var PushNotification = require("react-native-push-notification");

export class Participant extends React.Component {

componentWillMount() {
    PushNotification.configure({
        onNotification: (notification) => {

            //Code to print array into console
            console.log(notification);

            //Code to be executed according to the action selected by the user
            if (notification.action == "Copy ID") {
                Clipboard.setString('hello id');
            }
            else if (notification.action == "Copy Password") {
                Clipboard.setString('hello password');
            }
        },
        popInitialNotification: true,
        requestPermissions: true,
    });
}

//Function to generate count down timer
generateCountdownTimer = (index, eventDate, eventTime) => {

            return <CountDown style={{ marginBottom: 5, }} until={eventTimeIntoSeconds} size={17}
                onFinish={() => {

                    //Code to display push notification to the user into status bar
                    PushNotification.localNotification({
                        id: index,
                        largeIcon: "ic_launcher",
                        title: "1 new notification",
                        message: //Your msg here
                        vibrate: true,
                        vibration: 300,
                        priority: "high",
                        actions: '["Copy ID", "Copy Password"]',

                    });
                }
                } />
        }

}`

Isn't working. The onNotification only called when i click on the notification and not when notification appear (both iOS and android. Any solution?

yeah, nothing happens in the onNotification listener when I click on one of the actions...

+1 For me, on Android the notification stays in the system tray or notification tray after tapping a button. It does get dismissed from the foreground after its auto timeout, but like I said, it stays in the tray. This is on the emulator at least. Haven't gone as far as looking on a real device.

I do get the onNotification, callback to hit, but using a codeblock like this does nothing and does not get hit:

(function() {
  // Register all the valid actions for notifications here and add the action handler for each action
  PushNotificationAndroid.registerNotificationActions(['View Updates','Cancel']);
  DeviceEventEmitter.addListener('notificationActionReceived', function(action){
    console.log ('Notification action received: ' + action);
    const info = JSON.parse(action.dataJSON);
    if (info.action == 'View Updates') {
      // Do work pertaining to Accept action here
      console.log ('notificationActionReceived() :: VIEW UPDATES');
    } else if (info.action == 'Cancel') {
      // Do work pertaining to Reject action here
      console.log ('notificationActionReceived() :: CANCEL NOTIFICATION');
    }
    // Add all the required actions handlers
  });
})();
Was this page helpful?
0 / 5 - 0 ratings