pushy / pushy-react-native

The official Pushy SDK for React Native apps.
Apache License 2.0
16 stars 20 forks source link

On iOS - pushy-react-native does not work with @react-native-firebase/auth #63

Closed madebydavid closed 3 years ago

madebydavid commented 3 years ago

We are trying to get pushy-react-native to work along side @react-native-firebase/auth.

When calling firebase.auth() we would receive an error in the console:

[Error: [auth/notification-not-forwarded] If app delegate swizzling is disabled, remote notifications received by UIApplicationDelegate need to be forwarded to FIRAuth’s canHandleNotificaton: method.]

We attempted to resolve this by:

  1. Disabling swizzling in firebase by setting FirebaseAppDelegateProxyEnabled to NO in Info.plist
  2. Patching the PushyModule code as described in this comment on issue #56
  3. Following the notes on the firebase docs here
  4. Following the notes from the Pushy docs here - although the code included there is Swift and we would need Objective-C and the Pushy function toggleMethodSwizzling no-longer exists

However, we have not been successful.

Is there a guide or some notes on how to modify AppDelegate.m so that the current React Native version of Pushy can work with @react-native-firebase/auth or are the two libraries currently incompatible? Many of the examples or comments we can find seem to be for older versions of the libraries.

Many thanks!

pushy commented 3 years ago

Hi @madebydavid, Apologies for the difficulties you have been facing with getting these two libraries to work together. We'd be glad to assist.

1) In version 1.0.17, a change has been made where AppDelegate swizzling is forced, making it impossible to override the AppDelegate APNs methods in your AppDelegate.m. Please downgrade to 1.0.16 to work around this, by running these commands:

npm install pushy-react-native@1.0.16 --save
react-native link pushy-react-native
cd ios && pod install

2) Edit Libraries/PushyRN.xcodeproj/PushyModule.h, replacing it with the following:

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface PushyModule : RCTEventEmitter <RCTBridgeModule>
+ (void)didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token;
+ (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
@end

3) Edit Libraries/PushyRN.xcodeproj/PushyModule.m, adding the following method inside:

+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token {
    [pushy application:[UIApplication sharedApplication] didRegisterForRemoteNotificationsWithDeviceToken:token];
}

+ (void)didReceiveRemoteNotification:(NSDictionary *)notification fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [pushy application:[UIApplication sharedApplication] didReceiveRemoteNotification:notification fetchCompletionHandler:completionHandler];
}

4) Add the following method overrides to your AppDelegate.m (delete previous overrides for these two methods, if exist):

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Pass token to Firebase Auth
  [[FIRAuth auth] setAPNSToken:deviceToken type:FIRAuthAPNSTokenTypeSandbox];

  // Pass token to Pushy
  [PushyModule didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)notification
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  // Pass to Firebase Auth
  if ([[FIRAuth auth] canHandleNotification:notification]) {
    completionHandler(UIBackgroundFetchResultNoData);
    return;
  }

  // Pass notification to Pushy
  [PushyModule didReceiveRemoteNotification: notification fetchCompletionHandler: completionHandler];
}

Run your app, and let us know if the auth/notification-not-forwarded error goes away.