zoho / SalesIQ-Mobilisten-ReactNative

Your mobile app's ideal live chat partner. Power up your mobile App with the SalesIQ Mobilisten SDK for React-Native.
https://mobilisten.io/
8 stars 12 forks source link

Remote notification registration on iOS (Notifee plugin) #29

Closed hungkoala closed 3 years ago

hungkoala commented 3 years ago

SalesIQ blocks notifee to show notifications. If I remove all SalesIQ code in AppDelegate.m the application works well

Here is the code


#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "RNBootSplash.h" // react native bootsplash
#import <Firebase.h> // react native firebase
#import <React/RCTLinkingManager.h> // react native auth0
#import <AppCenterReactNative.h> // appcenter
#import <AppCenterReactNativeAnalytics.h> // appcenter
#import <AppCenterReactNativeCrashes.h> // appcenter
#import <AppCenterReactNative.h> // appcenter
#import <CodePush/CodePush.h> // codepush
#import "Bewell-Swift.h"
#import "RNFBMessagingModule.h"

#ifdef FB_SONARKIT_ENABLED
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>

static void InitializeFlipper(UIApplication *application) {
  FlipperClient *client = [FlipperClient sharedClient];
  SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
  [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
  [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
  [client addPlugin:[FlipperKitReactPlugin new]];
  [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
  [client start];
}
#endif

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef FB_SONARKIT_ENABLED
  InitializeFlipper(application);
#endif
  if ([FIRApp defaultApp] == nil) { // rn firebase
      [FIRApp configure];
    }
  [AppCenterReactNative register]; // appcenter
  [AppCenterReactNativeAnalytics registerWithInitiallyEnabled:true]; // appcenter
  [AppCenterReactNativeCrashes registerWithAutomaticProcessing]; // appcenter

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];

  // NSDictionary *appProperties = [RNFBMessagingModule addCustomPropsToUserProps:nil withLaunchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"Bewell"
                                            initialProperties:nil];

  if (@available(iOS 13.0, *)) {
      rootView.backgroundColor = [UIColor systemBackgroundColor];
  } else {
      rootView.backgroundColor = [UIColor whiteColor];
  }

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];

  [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; // <- initialization using the storyboard file name
  LaunchScreen *t = [LaunchScreen new];
  UIView *animationView = [t createAnimationViewWithRootView:rootView lottieName:@"launch_screen_animation"];
  [rootView addSubview:animationView];
  [t playWithAnimationView:animationView];

  //Sales IQ Push notification
  if (@available(iOS 10.0, *)) {
        //  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //  [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        //    center.delegate = self;
        //    if (granted){
        //     //  dispatch_async(dispatch_get_main_queue(), ^{
        //       // UNNotificationCategoryOptions categoryOptions = UNNotificationCategoryOptionNone;
        //       // UNTextInputNotificationAction *reply = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:@[] textInputButtonTitle:@"Send" textInputPlaceholder:@"Message"];
        //       // UNNotificationCategory *chatGrpMsgCategory_10 = [UNNotificationCategory categoryWithIdentifier:@"GRPCHATMSG" actions:@[reply] intentIdentifiers:@[] options:categoryOptions];
        //       // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:chatGrpMsgCategory_10,chatGrpMsgCategory_10, nil]];
        //       // [application registerForRemoteNotifications];
        //     //  });
        //    }
        //  }];
    }
    else{
         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
         [application registerUserNotificationSettings:settings];
    }
    if(launchOptions[@"UIApplicationLaunchOptionsRemoteNotificationKey"] != nil){
      NSDictionary *notificationData = launchOptions[@"UIApplicationLaunchOptionsRemoteNotificationKey"];
      if(notificationData != nil){
        [RNZohoSalesIQ processNotificationWithInfo:notificationData];
      }
    }
  return YES;
}

// - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//   const char *data = [deviceToken bytes];
//   NSMutableString *token = [NSMutableString string];
//   for (NSUInteger i = 0; i < [deviceToken length]; i++) {
//     [token appendFormat:@"%02.2hhX", data[i]];
//   }

//   NSLog(@"----------didRegisterForRemoteNotificationsWithDeviceToken:%@",token);
//   #ifdef DEBUG
//     [RNZohoSalesIQ enablePush:token isTestDevice:YES isProductionMode:NO];
//   #else
//     [RNZohoSalesIQ enablePush:token isTestDevice:NO isProductionMode:YES];
//   #endif
// }

// - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler {

//   if (application.applicationState == UIApplicationStateInactive) {

//     NSLog(@"----------Inactive - the user has tapped in the notification when app was closed or in background, %@",userInfo);
//     // do some tasks
//     [RNZohoSalesIQ processNotificationWithInfo:userInfo];
//     completionHandler(UIBackgroundFetchResultNewData);
//   } else if (application.applicationState == UIApplicationStateBackground) {

//     NSLog(@"------------application Background - notification has arrived when app was in background");
//     NSString *contentAvailable = [NSString
//         stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"]
//                                     valueForKey:@"content-available"]];

//     if ([contentAvailable isEqualToString:@"1"]) {
//       // do tasks
//       [RNZohoSalesIQ processNotificationWithInfo:userInfo];
//       NSLog(@"-----------content-available is equal to 1");
//       completionHandler(UIBackgroundFetchResultNewData);
//     }
//   } else {
//     NSLog(@"-----------application Active - notication has arrived while app was opened");
//     // Show an in-app banner
//     // do tasks
//     [RNZohoSalesIQ processNotificationWithInfo:userInfo];
//     completionHandler(UIBackgroundFetchResultNewData);
//   }
// }

// - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//   NSLog(@"-----------didReceiveLocalNotification");
//   [RNZohoSalesIQ processNotificationWithInfo:notification.userInfo];
// }

// - (void)didReceiveNotificationResponse:(UNNotificationResponse *)response {
//   NSLog(@"-----------didReceiveNotificationResponse");
//   if (@available(iOS 10.0, *)) {
//     NSLog(@"--------------------ios 10.0");
//     if ([RNZohoSalesIQ isMobilistenNotification:response.notification.request.content.userInfo]) {
//       NSLog(@"-----------------------------------isMobilistenNotification");
//       NSLog(@"-----------------------------------actionIdentifier:%@",response.actionIdentifier);
//        if ([response.actionIdentifier isEqualToString: @"Reply"]) {
//            [RNZohoSalesIQ handleNotificationAction:response.notification.request.content.userInfo response:((UNTextInputNotificationResponse *)response).userText];
//        }
//       else if([response.actionIdentifier isEqualToString: UNNotificationDefaultActionIdentifier]){
//            [RNZohoSalesIQ processNotificationWithInfo:response.notification.request.content.userInfo];
//       }
//     }
//   }
// }

// - (void)userNotificationCenter:(UNUserNotificationCenter *)center
//        willPresentNotification:(UNNotification *)notification
//          withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
//   [RNZohoSalesIQ processNotificationWithInfo:notification.request.content.userInfo];
// }

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:
        (UIUserNotificationSettings *)notificationSettings {
  // register to receive notifications
  [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application
    handleActionWithIdentifier:(NSString *)identifier
         forRemoteNotification:(NSDictionary *)userInfo
             completionHandler:(void (^)())completionHandler {
  // handle the actions
  if ([identifier isEqualToString:@"declineAction"]) {
  } else if ([identifier isEqualToString:@"answerAction"]) {
  }
}
#endif

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
  return [CodePush bundleURL];
#endif
}

// Auth0 signin open url
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
  return [RCTLinkingManager application:app openURL:url options:options];
}

@end
hungkoala commented 3 years ago

exactly these lines cause issue

//  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //  [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        //    center.delegate = self;
        //    if (granted){
        //     //  dispatch_async(dispatch_get_main_queue(), ^{
        //       // UNNotificationCategoryOptions categoryOptions = UNNotificationCategoryOptionNone;
        //       // UNTextInputNotificationAction *reply = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:@[] textInputButtonTitle:@"Send" textInputPlaceholder:@"Message"];
        //       // UNNotificationCategory *chatGrpMsgCategory_10 = [UNNotificationCategory categoryWithIdentifier:@"GRPCHATMSG" actions:@[reply] intentIdentifiers:@[] options:categoryOptions];
        //       // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:chatGrpMsgCategory_10,chatGrpMsgCategory_10, nil]];
        //       // [application registerForRemoteNotifications];
        //     //  });
        //    }
        //  }];
Rishabh-Raghunath commented 3 years ago

exactly these lines cause issue

//  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //  [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        //    center.delegate = self;
        //    if (granted){
        //     //  dispatch_async(dispatch_get_main_queue(), ^{
        //       // UNNotificationCategoryOptions categoryOptions = UNNotificationCategoryOptionNone;
        //       // UNTextInputNotificationAction *reply = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:@[] textInputButtonTitle:@"Send" textInputPlaceholder:@"Message"];
        //       // UNNotificationCategory *chatGrpMsgCategory_10 = [UNNotificationCategory categoryWithIdentifier:@"GRPCHATMSG" actions:@[reply] intentIdentifiers:@[] options:categoryOptions];
        //       // [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:chatGrpMsgCategory_10,chatGrpMsgCategory_10, nil]];
        //       // [application registerForRemoteNotifications];
        //     //  });
        //    }
        //  }];

The above snippet of code is just responsible for registering the application for remote notifications and is not directly related to SalesIQ Mobilisten. This step is required to receive the token used for remote notifications.

We see that Firebase is being used in the application and as per the firebase documentation, it is required to register the application for remote notification as done in the above snippet. Do you happen to be registering the application for remote notifications elsewhere and do remote notifications work for you without these lines?

hungkoala commented 3 years ago

Hi, It'a react-native app and I followed this guideline https://rnfirebase.io/messaging/usage

Currently at foreground mode, if i turn off the register lines above, Notifee will show notification popups. If if I enable register code, application will show nothing

Rishabh-Raghunath commented 3 years ago

It is likely that UNUserNotificationCenter's delegate property is being set by Notifee elsewhere. Could you try commenting out the center.delegate = self; line alone and see if it everything works?

If yes, could you try adding the below code to application(_:didFinishLaunchingWithOptions:) method as the first line and then try running the application?

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;

To add, you can use the RNZohoSalesIQ.isMobilistenNotification method to check if the notification is intended to be handled by Mobilisten by passing the notification content, else the notification would need to be handled by Notifee.

hungkoala commented 3 years ago

The function requestAuthorizationWithOptions override permission required by Notifee notifee.requestPermission() see requestpermission that causes the issue. If the line // [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { is removed, application will work well