react-native-push-notification's configure method is not return data. Data is coming null . How can i fix it?

359 Views Asked by At

react-native-push-notification's configure method is not return data. Data is coming null .Here is my code

React.useEffect(() => {
    PushNotification.configure({
      onNotification(notification) {
        if (notification.userInteraction) {
          console.log(notification);
          console.log('hi');
        }
        var nottype = notification.data.actionId;
        var notid = notification.data.id;

        console.log(notid);
        AsyncStorage.setItem('actionId', nottype);
        AsyncStorage.setItem('orderId', notid);
      },
    });
  }, []);

1

There are 1 best solutions below

3
Engr.Aftab Ufaq On

you are using react-native-push-notification this library is not the best to handle notification. instead you can use @notifee/react-native to configure this for notification. this is very easy to use and do not need native code configuration. to create a group or single channel

// Create a group
await notifee.createChannelGroup({
  id: 'personal',
  name: 'Personal',
});

// Assign the group to the channel
await notifee.createChannel({
  id: 'comments',
  name: 'New Comments',
  groupId: 'personal',
});

here is a sample

import notifee, {
  AndroidBadgeIconType,
  EventType,
  TriggerType,
} from '@notifee/react-native';

const onDisplayNotification = async (item, distance, message) => {
  try {
    await notifee.requestPermission();
    const channelId = await notifee.createChannel({
      id: 'default',
      name: 'Default Channel',
    });
    await notifee.displayNotification({
      title: `You are getting ${message} ${
        message == 'Close' ? 'to' : 'from'
      } ${item.name}`,
      body: `You are ${distance} M away form ${item.name}`,
      android: {
        channelId,
        actions: [
          {
            title: 'Close',
            pressAction: {
              id: 'stop',
            },
          },
        ],

        smallIcon: 'ic_launcher',
        pressAction: {
          id: 'default',
        },
      },
    });
  } catch (e) {
    console.error(e, 'ERRORORORORORORO');
  }
};

here is the event with which you can interreact with them .

useEffect(() => {
    return notifee.onForegroundEvent(({ type, detail }) => {
      switch (type) {
        case EventType.DISMISSED:
          console.log('User dismissed notification', detail.notification);
          break;
        case EventType.PRESS:
          console.log('User pressed notification', detail.notification);
          break;
      }
    });
  }, []);