OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 374 forks source link

Clash with expo-notifications (for local notifications) on iOS #1480

Open jacklj opened 1 year ago

jacklj commented 1 year ago

What happened?

I've got react-native-onesignal installed on a bare React Native project, and I've since installed expo-notifications to fire local notifications. I get this warning on iOS startup (it comes from this code here:

EXNotificationCenterDelegate encountered already present delegate of UNUserNotificationCenter: <OSUNUserNotificationCenterDelegate: 0x600003c68460>. EXNotificationCenterDelegate will not overwrite the value not to break other features of your app. In return, expo-notifications may not work properly. To fix this problem either remove setting of the second delegate or set the delegate to an instance of EXNotificationCenterDelegate manually afterwards.

I believe this is because OneSignal already sets its own delegate.

The result is: local notifications get delivered, but the foregrounded app notification handler doesn’t fire (nor does expo-notifications’ one).

To fix, I've tried to follow the advice given by the expo-notifications warning - set the delegate to an instance of EXNotificationCenterDelegate manually afterwards.:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // ... 

  EXNotificationCenterDelegate *expoNotifDelegate = [[EXNotificationCenterDelegate alloc] init];
  [[UNUserNotificationCenter currentNotificationCenter] setDelegate:expoNotifDelegate];

  //...
}

This builds successfully but then local notifications don't work at all.

This should be compatible with OneSignal, see here https://github.com/OneSignal/react-native-onesignal/issues/958#issuecomment-600267864:

OneSignal does swizzle the UNUserNotificationCenter so even if you have [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; in your code it shouldn't break OneSignal.

Any ideas how to make expo-notifications work with OneSignal on iOS (it works fine on Android)? Thanks

Steps to reproduce?

1. Install onesignal-react-native
2. Install expo-notifications
3. Schedule local notifications on iOS - the legacy onesignal native handler fires, neither JS handlers fire 
4. Try and set the delegate manually - it breaks local notifs completely

What did you expect to happen?

They should be compatible, as onesignal-react-native doesn’t implement local notifs, so we need to be able to use other libraries to implement them

React Native OneSignal SDK version

Release 4.5.0

Which platform(s) are affected?

Relevant log output

EXNotificationCenterDelegate encountered already present delegate of UNUserNotificationCenter: <OSUNUserNotificationCenterDelegate: 0x600003c68460>. EXNotificationCenterDelegate will not overwrite the value not to break other features of your app. In return, expo-notifications may not work properly. To fix this problem either remove setting of the second delegate or set the delegate to an instance of EXNotificationCenterDelegate manually afterwards.

Code of Conduct

brismithers commented 1 year ago

Hi @jacklj thank you for reporting, we will look into this and let you know what we find. Can you show where you initialize the OneSignal SDK in relation to your didFinishLaunchingWithOptions code above?

brismithers commented 1 year ago

@jacklj I did a quick edit to my initial message and not sure you saw it. Can you show where you initialize the OneSignal SDK in relation to your didFinishLaunchingWithOptions code above? Thanks!

jacklj commented 1 year ago

Thanks @brismithers, didn't see the update.

We followed the docs here https://documentation.onesignal.com/docs/react-native-sdk-setup - there is no OneSignal setup code in AppDelegate, just the OneSignalNotificationServiceExtension and then the initialization code in Javascript (in App.js).

brismithers commented 1 year ago

@jacklj confirming that I am seeing the same thing, working through why/what needs to change

brismithers commented 1 year ago

Hey @jacklj I don't have a good update, just letting you know I continue to try to figure this out. For context, I'm having trouble getting expo-notifications without onesignal-react-native to deliver a local notification while the app is in the foreground (local notifications while in background are delivered successfully). I think I'm running up against this issue.

I will mention one thing. In your original report you indicated the OneSignal notification received handler does not receive control when a local notification is received. I can say this is expected behavior. The OneSignal notification received handler will only get control for notifications sent by OneSignal.

So you must rely on expo's notification received handler for local notifications. And this is where I am currently stuck :).

Will keep at it!

radoam1 commented 1 year ago

I have similar problem when I have a both installed a listeners form expo-notifications don't work, any progres on this issue ?

Klyado commented 11 months ago

Hi there,

I've got the exact same issue. Any news about this @brismithers ?

neilljordan commented 3 months ago

Same here. Any suggestions?