ios icon indicating copy to clipboard operation
ios copied to clipboard

Can't recived notification when foreground

Open anhdo9797 opened this issue 4 years ago • 6 comments

dependencies:

  • react-native: 0.63.4
  • react-native-push-notification-ios: 1.8.0

My issue: I received Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called. when my app has notification from fcm in the foreground

My AppDelegate.m:

` ... @implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { #ifdef FB_SONARKIT_ENABLED InitializeFlipper(application); #endif

    RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"<My_app>" 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];

    if ([FIRApp defaultApp] == nil) { [FIRApp configure]; }

    [RNSplashScreen show]; [GMSServices provideAPIKey:GOOGLE_API_KEY];

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

    return YES; }

  • (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif }

// Required for the register event.

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } // 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 { [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; } // Required for the registrationError event.
  • (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error]; } // Required for localNotification event
  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { [RNCPushNotificationIOS didReceiveNotificationResponse:response]; } //Called when a notification is delivered to a foreground app. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge); } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { [RNCPushNotificationIOS didReceiveLocalNotification:notification]; }

@end `

anhdo9797 avatar May 23 '21 09:05 anhdo9797

Hello, I also faced this issue, please help to give solution!!!

perfectAlways1028 avatar May 25 '21 20:05 perfectAlways1028

Hello, I also faced this issue, please help to give solution!!!

i found the solution that i added [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; the end function didReceiveRemoteNotification. It is the line 147 in my code.

Screen Shot 2021-05-26 at 09 13 12

anhdo9797 avatar May 26 '21 02:05 anhdo9797

@anhdo9797 I am still getting the same

Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

AppDelegate.m method updated

// 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 { [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; }

Adichilla avatar Jan 10 '22 13:01 Adichilla

Hello, I also faced this issue, please help to give solution!!!

i found the solution that i added [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; the end function didReceiveRemoteNotification. It is the line 147 in my code.

Screen Shot 2021-05-26 at 09 13 12

thanks a lot it worked for me

salik-a avatar Mar 16 '22 10:03 salik-a

Just comment the below function

// Required for localNotification event /void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { [RNCPushNotificationIOS didReceiveNotificationResponse:response]; }

than this Function will start working **(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

JatinPreetAxtria avatar Apr 28 '22 06:04 JatinPreetAxtria