4

I'm using this package for implementing local push notification:

https://github.com/zo0r/react-native-push-notification

I'm using action button like this to show buttons in my notification along with a text and a title:

PushNotification.localNotification({
...
actions: '["Yes", "No"]'
})

I wanted to know how I can call a function when user clicks on of these actions and app becomes visible?

I've tried PushNotification.configure in my componentDidMount method in my home screen like this but nothing comes up in the console:

  PushNotification.configure({
    // (required) Called when a remote or local notification is opened or received
    onNotification: function(notification) {
      console.log("NOTIFICATION:", notification);
      if (notification.userInteraction) {
        console.log("NOTIFICATION:");
      }

      // process the notification
    }
  });
3iL
  • 2,146
  • 2
  • 23
  • 47

4 Answers4

3

I got it working.

In your App.js you need to set popInitialNotification to true. Something like this:

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

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

      // Should the initial notification be popped automatically
      // default: true
      popInitialNotification: true,

      /**
       * (optional) default: true
       * - Specified if permissions (ios) and token (android and ios) will requested or not,
       * - if not, you must call PushNotificationsHandler.requestPermissions() later
       */
      requestPermissions: true
    });
  }

notification.action will gibe you the label of the button clicked.

3iL
  • 2,146
  • 2
  • 23
  • 47
  • 2
    am I right to say the `onNotification` works only for **Android**? I tried in **iOS**, the `onNotification` is not triggered when I open a remote notification – Tommy Leong Sep 09 '20 at 07:04
1

In your button/app active event you forgot to call to schedule the notification and actually set when it will arise, so you need to

PushNotification.localNotificationSchedule(details: Object)

Schedule it for now with the same id, then your notification will come up immediately.

See all options for scheduling here

gran33
  • 12,421
  • 9
  • 48
  • 76
  • 1
    I have already done that. My notification is coming on scheduled time. That isn't the problem. The problem is that I two buttons on my notification and I just want to perform onpress/onclick on them. – 3iL Aug 24 '19 at 10:26
1
import PushNotificationAndroid from 'react-native-push-notification'

(function() {
  // Register all the valid actions for notifications here and add the action handler for each action
  PushNotificationAndroid.registerNotificationActions(['Accept','Reject','Yes','No']);
  DeviceEventEmitter.addListener('notificationActionReceived', function(action){
    console.log ('Notification action received: ' + action);
    const info = JSON.parse(action.dataJSON);
    if (info.action == 'Accept') {
      // Do work pertaining to Accept action here
    } else if (info.action == 'Reject') {
      // Do work pertaining to Reject action here
    }
    // Add all the required actions handlers
  });
})();
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
0

DO NOT USE .configure() INSIDE A COMPONENT, EVEN App

If you do, notification handlers will not fire, because they are not loaded. Instead, use .configure() in the app's first file, usually index.js.

It's mentioned in the documentation.

Try to follow their example for implementation It will help you to setup in your project.

MithlEsh
  • 1
  • 2