OneSignal / onesignal-expo-plugin

The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.
Other
142 stars 46 forks source link

[Bug]: addEventListener doesn't work #227

Closed daniimd closed 1 week ago

daniimd commented 4 months ago

What happened?

When sending a PUSHthrough 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

opmat commented 4 months ago

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

rgomezp commented 1 week ago

Thanks @opmat ,

Closing due to inactivity from OP