OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.56k stars 371 forks source link

[Bug]: Duplicate notifications occur when using an additional SDK, and foregroundWillDisplay does not fire with notifications from sources outside of OneSignal. #1667

Open marcin-piela-800 opened 4 months ago

marcin-piela-800 commented 4 months ago

What happened?

Version:

"react-native-onesignal": "^5.0.6",

We're using a different package to handle push notifications from our backend. Everything works fine except we can't prevent duplicate notifications from showing when the app is in the foreground. The problem arises because the event listener for foreground notifications

 OneSignal.Notifications.addEventListener('foregroundWillDisplay', (event) => {
      console.log(event)
    })

is never fired for push notifications not coming from OneSignal (it is fired for OneSignal notification). However, OneSignal is firing for those notifications:

VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler:

So notification is visible to the user and there is no any way for us to control it, basically we want to add some logic to skip such notifications.

Steps to reproduce?

1. Install all dependencies and setup OneSignal SDK
2. Send a push notification from different provider (for example Pusher)

What did you expect to happen?

OneSignal should fire foregroundWillDisplay to all push notifications not just OneSignal notifications, or it shouldn't show them when app is in the foreground

React Native OneSignal SDK version

5.1.0

Which platform(s) are affected?

Relevant log output

1. App is foreground and notification is recieved. Show a alert.
VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler:

Code of Conduct

jennantilla commented 4 months ago

Hi @marcin-piela-800 thanks for reaching out! Just to make sure I understand, only the notifications from the other push provider display? If so, that is what I would expect as our methods are meant to handle OneSignal notifications.

I'll look a bit deeper at the logic and see if there is anything we can tweak to handle notifications not coming from OneSignal.

Thanks

marcin-piela-800 commented 4 months ago

I implemented the following method in AppDelegate.mm:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSDictionary *userInfo = notification.request.content.userInfo;
  NSString *customValue = userInfo[@"custom"]; // Added only by OneSignal

  if (customValue != nil) {
      completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
  } else {
      completionHandler(UNNotificationPresentationOptionNone);
  }
}

Now, I'm able to prevent notifications (other than OneSignal notifications) from being displayed when the app is in the focused state.

The problem is that it's not default behavior to show all notifications when app is focused, it's added by OneSignal so I would expect to be able to do smth with it using JS code (listener from OneSignal package)