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

Issues when integrated with Amplify push notifications #286

Closed
AleksandrTermenzhy opened this issue Jul 3, 2024 · 5 comments
Closed

Issues when integrated with Amplify push notifications #286

AleksandrTermenzhy opened this issue Jul 3, 2024 · 5 comments

Comments

@AleksandrTermenzhy
Copy link

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

mrehan27 commented Jul 3, 2024

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:

  • Initialize push module by calling MessagingPushAPN.initialize and disable auto registering of device tokens as follows:
MessagingPushAPN.initialize { config in
    config.autoFetchDeviceToken = false 
}
  • Modify AppDelegate and MyAppPushNotificationsHandler to match the implementation required before auto device token feature was released. I can help you find some relevant code using git history from our sample apps for AppDelegate and MyAppPushNotificationsHandler files.

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

@AleksandrTermenzhy
Copy link
Author

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

mrehan27 commented Jul 4, 2024

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

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

mrehan27 commented Jul 4, 2024

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

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

No branches or pull requests

3 participants