onesignal-expo-plugin icon indicating copy to clipboard operation
onesignal-expo-plugin copied to clipboard

[Bug]: addEventListener doesn't work

Open daniimd opened this issue 1 year ago • 1 comments

What happened?

When sending a PUSH through onesignal, I receive the notification normally, but when clicking on it I cannot get any action, I tried using addEventListener, but an error is returned

Code:

  OneSignal.Notifications.addEventListener((event) => {
    console.log('OneSignal: notification clicked:', event);
  })

Error: image

Version:

"onesignal-expo-plugin": "^2.0.2",
"react-native-onesignal": "^5.0.6",

Steps to reproduce?

1. Install one signal dependencies
2. Receive PUSH on the emulator
3. Tap the received notification
4. Function returns an error

What did you expect to happen?

I hope some function returns an action for tapping the notification

OneSignal Expo SDK version

49.0.6

Platform

Android

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

daniimd avatar Feb 23 '24 14:02 daniimd

Hi @daniimd From the error message, it seems OneSignal is not yet initialized before the call to add event listerner.

Furthermore, if this is your code

  OneSignal.Notifications.addEventListener((event) => {
    console.log('OneSignal: notification clicked:', event);
  })

then the other issue is that you are not specifying the event you are adding listener for. Take a look at the examples below for notification click event;

OneSignal.Notifications.addEventListener("click", (event) => {
    console.log('OneSignal: notification clicked:', event);
  })

to get and display the notification on click, you can do this;

OneSignal.Notifications.addEventListener("click", (event) => {
    console.log('OneSignal: notification clicked:', event);
    const notification  = event.notification;
    if (notification === undefined) { return; }
    Alert.alert(notification.title, notification.body);  // You need to import Alert from react-native
  })

I hope this helps

opmat avatar Feb 26 '24 12:02 opmat

Thanks @opmat ,

Closing due to inactivity from OP

rgomezp avatar Jun 20 '24 18:06 rgomezp