ios icon indicating copy to clipboard operation
ios copied to clipboard

Notification not working in foreground but working in background

Open G10dRafaFan opened this issue 4 years ago • 8 comments

I have followed the document and checked several times.I am getting the notification when my app is in background , but I'm not receiving when the app is in foreground. Can anyone help me out with this? My appDelegate.m file: #import "AppDelegate.h" #import <UserNotifications/UserNotifications.h> #import <RNCPushNotificationIOS.h>

#import <React/RCTBridge.h> #import <React/RCTBundleURLProvider.h> #import <React/RCTRootView.h> #import <Firebase.h>

@import Firebase; #if DEBUG && TARGET_OS_SIMULATOR #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

// Required to register for notifications

  • (void)application:(UIApplication* )application didRegisterUserNotificationSettings:(UIUserNotificationSettings* )notificationSettings { [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings]; } // 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 the localNotification event.

  • (void)application:(UIApplication* )application didReceiveLocalNotification:(UILocalNotification* )notification { [RNCPushNotificationIOS didReceiveLocalNotification:notification]; }

  • (BOOL)application:(UIApplication* )application didFinishLaunchingWithOptions:(NSDictionary* )launchOptions { RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@“APPNAME initialProperties:nil];

rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; if ([FIRApp defaultApp] == nil) { [FIRApp configure]; } self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView;

self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible];

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; return YES; }

// Called when a notification is delivered to a foreground app. -(void)userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification:(UNNotification* )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { NSLog(@"User Info : %@",notification.request.content.userInfo); completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);

}

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

#if RCT_DEV

  • (BOOL)bridge:(RCTBridge* )bridge didNotFindModule:(NSString* )moduleName { return YES; } #endif

@end

My AppDelegate.h #import <UserNotifications/UNUserNotificationCenter.h> #import <React/RCTBridgeDelegate.h> #import <UIKit/UIKit.h>

// @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate> @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

G10dRafaFan avatar Jan 29 '21 15:01 G10dRafaFan

I have the same issue

bubaluza avatar Jan 29 '21 20:01 bubaluza

try this: https://github.com/react-native-push-notification-ios/push-notification-ios/issues/154#issuecomment-663827513

asherguedalia avatar Jan 31 '21 13:01 asherguedalia

try this: #154 (comment)

Worked!! For some reason the completionHandler was not trigging the event inside JS because if you place a print right on the foreground event handler on AppDelegate.m you will see the notification. The only inconvenience is that the action came undefined and you must filter the foreground notification by foreground and userInteraction property.

bubaluza avatar Jan 31 '21 16:01 bubaluza

I have added this to the AppDelegate.m

//Called when a notification is delivered to a foreground app.

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    NSDictionary *userInfo = notification.request.content.userInfo; //Foreground NSLog(@"APP_PUSH from foreground %@", userInfo);

    [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:^void (UIBackgroundFetchResult result){}]; completionHandler(UNNotificationPresentationOptionAlert);

} But still not working !!!

G10dRafaFan avatar Jan 31 '21 20:01 G10dRafaFan

I have added this to the AppDelegate.m

//Called when a notification is delivered to a foreground app.

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ NSDictionary *userInfo = notification.request.content.userInfo; //Foreground NSLog(@"APP_PUSH from foreground %@", userInfo); [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:^void (UIBackgroundFetchResult result){}]; completionHandler(UNNotificationPresentationOptionAlert);

} But still not working !!!

me too

anhdo9797 avatar May 12 '21 11:05 anhdo9797

UNNotificationPresentationOptionAlert is deprecated in iOS 14 and replaced by UNNotificationPresentationOptionList & UNNotificationPresentationOptionBanner.

This works for me (no notification alert when in foreground)

  NSDictionary *userInfo = notification.request.content.userInfo;

  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:^void (UIBackgroundFetchResult result){}];
  completionHandler(UNNotificationPresentationOptionNone);

If you do want the alert to also show when the app is in foreground, you can use

  NSDictionary *userInfo = notification.request.content.userInfo;
  //Foreground
  NSLog(@"APP_PUSH from foreground %@", userInfo);

  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:^void (UIBackgroundFetchResult result){}];
  if (@available(iOS 14.0, *)) {
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBanner | UNNotificationPresentationOptionBadge);
  } else {
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
  }

thijs-qv avatar Jun 18 '21 09:06 thijs-qv

@thijs-qv When I add above snippet to userNotificationCenter, onNotification it get execute in infinite loop on notification receive.

      -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification 
      withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
     { 
       //... your code(2) snip 
      }

sagarhudge-eaton avatar Aug 29 '23 05:08 sagarhudge-eaton

@sagarhudge-eaton have you managed to solve this? I have a console.log whenever I receive the notification and I also get it infinite times.

Thanx in advance.

ChrisFodor333 avatar Feb 27 '24 10:02 ChrisFodor333