customerio / customerio-reactnative

MIT License
23 stars 11 forks source link

Issues when integrated with Amplify push notifications #286

Closed AleksandrTermenzhy closed 1 week ago

AleksandrTermenzhy commented 1 week ago

SDK version: 3.7.2 React Native version: 0.73.8

Are logs available?

No

Describe the issue In our app we use AWS Amplify for sending push notifications. But it fails to work when used alongside with Customer.IO SDK. Essentially, it seems that the app never registers for push notifications. The main reason seems to be due to this line:

MessagingPushAPN.initialize(configOptions: nil)

Commenting this line allows for the app to work as expected and push notifications to arrive successfully. However, I assume it might cause some issue with tracking metrics for CustomerIO side.

I'll appreciate any thoughts on how to fix this.

-- Here is AppDelegate.mm and notifications handler for reference:

// AppDelegate.mm
// ...

@implementation AppDelegate

CustomerIOPushNotificationsHandler* pnHandlerObj = [[CustomerIOPushNotificationsHandler alloc] init];

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.moduleName = @"SnapNurseMobile";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};

  // Initialize CustomerIO SDK
  [pnHandlerObj setupCustomerIOClickHandling];

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [pnHandlerObj application:application deviceToken:deviceToken];

  [AmplifyPushNotification didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [pnHandlerObj application:application error:error];
}

// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [AmplifyPushNotification didReceiveRemoteNotification:userInfo withCompletionHandler:completionHandler];
}

@end
import Foundation
import CioMessagingPushAPN
import CioTracking

@objc
public class CustomerIOPushNotificationsHandler : NSObject {

  public override init() {}

  @objc(setupCustomerIOClickHandling)
  public func setupCustomerIOClickHandling() {
    CustomerIO.initialize(siteId: RNCConfig.env(for: "CUSTOMER_IO_SITE_ID"), apiKey: RNCConfig.env(for: "CUSTOMER_IO_API_KEY"), region: Region.US) { config in
      config.autoTrackDeviceAttributes = true
    }
    // The next line causes troubles
    // MessagingPushAPN.initialize(configOptions: nil)
  }

  @objc(application:deviceToken:)
  public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    MessagingPush.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
  }

  @objc(application:error:)
  public func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    MessagingPush.shared.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
  }
}
mrehan27 commented 1 week ago

Hi @AleksandrTermenzhy. Thanks for reaching out and sharing the details. Sorry to hear about the issue you're facing. This is probably similar to the one reported on Expo? If yes, we are aware of it and do have plans to improve experience for such use cases. While the fix might not be available very soon, there is a workaround that should help you solve the issue.

To make it work, you need to follow these steps:

MessagingPushAPN.initialize { config in
    config.autoFetchDeviceToken = false 
}

Let me know if this doesn't solve the issue or if you have more questions. Have a great day.

AleksandrTermenzhy commented 1 week ago

Hey @mrehan27 Thanks for quick reply. Can you provide more details on what changes to AppDelegate and MyAppPushNotificationsHandler exactly should I be looking at? Cause that code is for pre-0.73 RN version.

Also, should adding config.autoFetchDeviceToken = false only work in my case?

mrehan27 commented 1 week ago

Sorry if this has caused any confusion @AleksandrTermenzhy. I checked with team to confirm, and you don't need to make other changes. Only disabling auto-fetching of token by adding following code in setupCustomerIOClickHandling of CustomerIOPushNotificationsHandler should work for you:

MessagingPushAPN.initialize { config in
    config.autoFetchDeviceToken = false 
}

Do let us know if this solves the problem for you or not. We can dive deeper and help if this doesn't fix the problem for you. Please feel free to ask any more questions you have. Have a great day.

AleksandrTermenzhy commented 1 week ago

Setting config.autoFetchDeviceToken = false seems to have fixed that issue for me. At least, the app registers successfully with Amplify now. I think we may close this issue for now.

Thanks for your help @mrehan27 !

mrehan27 commented 1 week ago

Thanks for the confirmation @AleksandrTermenzhy. Glad it worked out. Happy to help!