CleverTap/clevertap-react-native

iOS notification in Foreground

Closed this issue · 10 comments

cannot receive push notifications in foreground & docs are not clear.

@ramssutharr can you please provide some details here so that we can help?

  • CleverTap React Native SDK version
  • Platform - Android/iOS?
  • Documentation links?
  • Error logs/screenshots?

@darshanclevertap i am facing the issue on iOS i have followed the steps provided on official website of clevertap and there is no error. the only issue is im not receiving notifications in foreground , im using latest sdk of RNCleverTap. and also there are syntax error when setting up the iOS part in step 4 - https://developer.clevertap.com/docs/push-notifications-ios . i have already firebase notifications configured with notifee library which handle both case and also Notification Extension Service is added for foreground notification. does it has to do anything with cleverTap integration ?

Hi @ramssutharr,

Can you follow the steps mentioned here to get notifications in foreground and let me know if it works or not. You need to add one delegate method in your AppDelegate to get notifications in foreground: https://developer.clevertap.com/docs/react-native-push-notification#ios-foreground-notifications

@nishant-clevertap done everything , still not getting notification in foreground. I’m getting notifications in foreground and background from firebase & notifee. also my existing notification extension service is getting triggered if i click on background notification received from cleverTap.

@ramssutharr Can you share your AppDelegate file if possible to check if everything is added?

#import "AppDelegate.h"
#import <Firebase.h>
#import "RNBootSplash.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTLinkingManager.h>
#import "CleverTap.h"
#import "CleverTapReactManager.h"
#import <CodePush/CodePush.h>
#import <UserNotifications/UserNotifications.h>
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  [self registerPush];
  self.moduleName = @"Currently";
  // 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 = @{};
  [CleverTap autoIntegrate]; // integrate CleverTap SDK using the autoIntegrate option
  [[CleverTapReactManager sharedInstance] applicationDidLaunchWithOptions:launchOptions];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)registerPush {
   
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if( !error ){
            dispatch_async(dispatch_get_main_queue(), ^(void) {
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            });
        }
    }];
}

-(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
  NSLog(@"%@: failed to register for remote notifications: %@", self.description, error.localizedDescription);
}

-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
  NSLog(@"%@: registered for remote notifications: %@", self.description, deviceToken.description);
}

-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    
  NSLog(@"%@: did receive notification response: %@", self.description, response.notification.request.content.userInfo);
  completionHandler();
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSLog(@"%@: will present notification: %@", self.description, notification.request.content.userInfo);
    [[CleverTap sharedInstance] recordNotificationViewedEventWithData:notification.request.content.userInfo];
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSLog(@"%@: did receive remote notification completionhandler: %@", self.description, userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)pushNotificationTappedWithCustomExtras:(NSDictionary *)customExtras{
  NSLog(@"pushNotificationTapped: customExtras: ", customExtras);
}

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

/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
///
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
/// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
/// @return: `true` if the `concurrentRoot` feature is enabled. Otherwise, it returns `false`.
- (BOOL)concurrentRootEnabled
{
  return true;
}

- (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
                          moduleName:(NSString *)moduleName
                           initProps:(NSDictionary *)initProps {
  UIView *rootView = [super createRootViewWithBridge:bridge
                                          moduleName:moduleName
                                           initProps:initProps];

  [RNBootSplash initWithStoryboard:@"BootSplash" rootView:rootView]; // ⬅️ initialize the splash screen

  return rootView;
}

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

- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
 return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                    restorationHandler:restorationHandler];
}

@end

@nishant-clevertap here it is

@ramssutharr
As I can see you have not conforms to UNUserNotificationCenterDelegate in AppDelegate as mentioned in the doc. You can refer this example on how to conforms delegate: https://github.com/CleverTap/CTNotificationService/blob/master/Example/CTNotificationServiceExample/AppDelegate.m#L6

Also you need to add the UNUserNotificationCenter delegate to self as added here on line no 24: https://github.com/CleverTap/CTNotificationService/blob/master/Example/CTNotificationServiceExample/AppDelegate.m#L24

Try adding these 2 changes then it will work.

@nishant-clevertap thank you so much got it working.

@nishant-clevertap after doing this I’m not getting any event inside this--> CleverTapPushNotificationClicked , and the eventis being received in firebase NotificationExtensionService after click. why so ? how can i avoid this ?

@ramssutharr I think you are using two frameworks for same thing and you can't use push notifications method for both frameworks at a time with method swizzle on. I recommend integrating CleverTap SDK manually and check if everything works properly or not if you want to use it with other frameworks. Here are docs to integrate SDK manually:
https://developer.clevertap.com/docs/ios-quickstart-guide#manual-installation
https://developer.clevertap.com/docs/push-notifications-ios#manually-integrating-push-notification-support