Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 of 3 tasks
marcin-piela-800 opened this issue Mar 18, 2024 · 2 comments

Comments

@marcin-piela-800
Copy link

marcin-piela-800 commented Mar 18, 2024

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?

  • iOS
  • Android

Relevant log output

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

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jennantilla
Copy link
Contributor

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
Copy link
Author

marcin-piela-800 commented Mar 19, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants