react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
740 stars 284 forks source link

Notification not working in foreground but working in background #256

Open G10dRafaFan opened 3 years ago

G10dRafaFan commented 3 years ago

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

import <React/RCTBridge.h>

import <React/RCTBundleURLProvider.h>

import <React/RCTRootView.h>

import

@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

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);

}

if RCT_DEV

@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

bubaluza commented 3 years ago

I have the same issue

asherguedalia commented 3 years ago

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

bubaluza commented 3 years ago

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.

G10dRafaFan commented 3 years ago

I have added this to the AppDelegate.m

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

} But still not working !!!

anhdo9797 commented 3 years ago

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

thijs-qv commented 3 years ago

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);
  }
sagarhudge-eaton commented 12 months ago

@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 
      }
ChrisFodor333 commented 5 months ago

@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.