'localNotification' event Handler is not fired from '@react-native-community/push-notification-ios'

341 Views Asked by At

I'm using PushNotificationIOS from '@react-native-community/push-notification-ios'; https://github.com/react-native-push-notification/ios/

I add local noti request in my app and the notification is delivered well but the event listener is not called.

    PushNotificationIOS.addNotificationRequest({
            id: 'asdfdf',
            title: 'dasdf',
            isCritical: true,
            isSilent: true,
            body: '일어나야지~~',
            fireDate: date,
        });

this is my code to send notification

useEffect(() => {
        console.log('uef');
        PushNotificationIOS.addEventListener('localNotification', () => {
            console.log('로컬 노티 왔어요~~');
        });

        return () => {
            PushNotificationIOS.removeEventListener('localNotification');
        };
    }, []);

this is how i wrote on App.tsx useeffect just to test if the handler is triggered.

I don't know if the trouble happens because of 'react-native-splash-screen'? i saw a issue mentioning this library..

My AppDelegate.h

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

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

@property (nonatomic, strong) UIWindow *window;

@end

My AppDelegate.mm


#import "AppDelegate.h"
// firebase
#import <Firebase.h>

#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "RNSplashScreen.h"

#import <React/RCTAppSetupUtils.h>

....



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
  // Define UNUserNotificationCenter
  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
{
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
// Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler
{
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
}

// 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];
}


Please help me figure out the problem ... how can others are using this feature with no problem? did I write something wrong in my appdelegate? :(

Does anybody know the solution, please?

Appdelegate revising..

expected event handlers to be called.

1

There are 1 best solutions below

0
Aiman Farhan On

https://github.com/zo0r/react-native-push-notification/issues/2185

Have a look at this thread. It helped me fix mine, I had to add the following to my appDelegate.

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  //THIS ONE, it allows you to call javascript even on foreground state.
  NSDictionary *userInfo = notification.request.content.userInfo;
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo];
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}