SoFunction
Updated on 2025-04-12

Reactnative-iOS callback Javascript method

Reactnative can call native modules, and native modules can also send event notifications to JavaScript. The best way is to inherit RCTEventEmitter. Customize the subclass RCTEventEmitter inherited from PushEventEmitter.

#import <Foundation/>
#import <React/>
#import <React/>

@interface PushEventEmitter : RCTEventEmitter <RCTBridgeModule>

- (void)addEventReminderReceived:(NSNotification *)notification;

@end

Implement supportedEvents method

#import ""

@implementation PushEventEmitter

+ (id)allocWithZone:(NSZone *)zone {
  static PushEventEmitter *sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [super allocWithZone:zone];
  });
  return sharedInstance;
}

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents
{
  return @[@"EventReminder"];
}

- (void)addEventReminderReceived:(NSNotification *)notification {
  [self sendEventWithName:@"EventReminder" body:@{@"name": @"FlyElephant"}];
}

@end

React native settings:

import {
  NativeModules,
  NativeEventEmitter,
} from 'react-native';

const PushEventEmitter = ;

const emitterManager = new NativeEventEmitter(PushEventEmitter);

Subscription notifications and removal notifications:

  componentDidMount() {
    subscription = (
      'EventReminder',
      (reminder) =&gt; ('JavaScript received notification:'+)
    );

  }
  componentWillUnmount(){
    ();// Remove notification  }

Call test:

PushEventEmitter *eventEmitter = [PushEventEmitter allocWithZone:nil];

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.