pushy/pushy-react-native

Allow APN token input on Register()

Closed this issue · 8 comments

Hi,

Firebase Messaging actually provides an APN token creation and integration with 3rd party push notification providers:

https://rnfirebase.io/messaging/server-integration

 // If using other push notification providers (ie Amazon SNS, etc)
// you may need to get the APNs token instead for iOS:
if(Platform.OS == 'ios') { messaging().getAPNSToken().then(token => { return addTokenToThirdParty; }); }

However, with Pushy it seems that I cannot register an APN token myself. Is it possible for Pushy to provide a feature whereby it allows APN tokens from Firebase, so that we can use Firebase and Pushy in the same app together?

pushy commented

Hi @causztic,
We'd be glad to assist with your inquiry.

Both the Pushy iOS SDK and the Firebase iOS SDK use method swizzling to hook into the AppDelegate APNs token registration methods to capture the APNs token, which causes a conflict when both SDKs are used in the same project.

To use both FCM and Pushy in the same iOS project:

  1. Please disable swizzling in the FCM SDK by adding the flag FirebaseAppDelegateProxyEnabled to your Info.plist and setting it to NO as described here:
    https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in

  2. Follow the instructions here to implement the didRegisterForRemoteNotificationsWithDeviceToken method manually in your React Native project's AppDelegate, calling the FCM method as documented:
    https://firebase.google.com/docs/cloud-messaging/ios/client#token-swizzle-disabled

  3. In the same implementation of didRegisterForRemoteNotificationsWithDeviceToken, add an invocation to pass the token the Pushy iOS SDK. Final code could look like this:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Pass token to Firebase
        Messaging.messaging().apnsToken = deviceToken
       
        // Call Pushy SDK didRegisterForRemoteNotificationsWithDeviceToken:deviceToken method
        Pushy(UIApplication.shared).application(application, didRegisterForRemoteNotificationsWithDeviceToken:deviceToken)
    }

Good day,
Can you tell me how this config will look like in React Native in the AppDelegate.m (Objective-C) file?

pushy commented

Hi @XfortunaX,
Simply open your React Native iOS project in Xcode (ios/.xcodeproj file), and edit the AppDelegate.m inside it, adding the following method override:

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
       // Pass token to Firebase
      [FIRMessaging messaging].APNSToken = deviceToken;

       // Pass token to Pushy
       Pushy* pushy = [[Pushy alloc]init:[UIApplication sharedApplication]];
      [pushy application:application, didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

Thanks for your support, but this code gives an error: Use of undeclared identifier 'Pushy'

I used the connection guide - https://pushy.me/docs/additional-platforms/react-native
And added the following lines
#import <PushyModule.h>

// Initialize Pushy Module
[PushyModule didFinishLaunchingWithOptions: launchOptions];

What needs to be added for this code to work?

pushy commented

Hi @XfortunaX,
Please use the following import statement to access the Pushy class in React Native:

#if __has_include(<PushyRN/PushyRN-Swift.h>)
#import <PushyRN/PushyRN-Swift.h>
#else
#import "PushyRN-Swift.h"
#endif

Hi,
I have added these imports to the top of AppDelegate.m next to
#import <PushyModule.h>

but gives an import error - 'PushyRN-Swift.h' file not found

I saw that these imports are used inside the module, but they are not available here ...

pushy commented

Hi @XfortunaX,
Please try this instead (and remove the import statement suggested previously:

  1. Use the following override of didRegisterForRemoteNotificationsWithDeviceToken in your project's AppDelegate.m:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Pass token to Firebase
  [FIRMessaging messaging].APNSToken = deviceToken;
  
  // Pass token to Pushy
  [PushyModule didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
  1. 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;
@end
  1. Edit Libraries/PushyRN.xcodeproj/PushyModule.m, adding the following method inside:
+ (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token {
    [pushy application:[UIApplication sharedApplication] didRegisterForRemoteNotificationsWithDeviceToken:token];
}

Thank you @pushy-me, it work!