1. 參考中文官網(wǎng)或者英文官網(wǎng)的代碼如下:
給Javascript發(fā)送事件
即使沒有被JavaScript調(diào)用,原生模塊也可以給JavaScript發(fā)送事件通知。最好的方法是繼承RCTEventEmitter,實(shí)現(xiàn)suppportEvents方法并調(diào)用self sendEventWithName:。
OC端:

RN端:

只是簡簡單單的幾行代碼,作為新手根本無法?calendarEventReminderReceived 這個(gè)方法到底如何使用?也沒有明確的說明
2.下面我來完完整整的實(shí)現(xiàn)了一下iOS端給RN端發(fā)送事件:
iOS端:
// xxx.h?
#import<React/RCTEventEmitter.h>
#import<React/RCTEventEmitter.h>
@interface RNPushEvent : RCTEventEmitter<RCTBridgeModule>
@end
//xxx.m
#import "RNPushEvent.h"
@implementation RNPushEvent
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
? return @[@"EventReminder"];
}
-(void)startObserving{
? [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(emitEventInternal:) name:@"event-emited" object:nil];
}
-(void)stopObserving{
? [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)emitEventInternal:(NSNotification*)notification{
? [self sendEventWithName:@"EventReminder" body:notification.userInfo];
}
+ (void)emitEventWithName:(NSString*)name andPayload:(NSDictionary*)payload
{
? [[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? object:self
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? userInfo:payload];
}
@end
RN端:
import React,{Component} from 'react';
import {
? ·
? ·
? ·
? NativeModules,
? NativeEventEmitter,
} from 'react-native';
const {RNPushEvent} = NativeModules
const rnPushEventEmitter = new NativeEventEmitter(RNPushEvent)
componentDidMount() {
? ? ? ? rnPushEventEmitter.addListener('EventReminder',(data)=> console.log("EventReminder:",data));
? ? }
componentWillUnmount(){
? ? ? ? this.rnPushEventEmitter.remove()
? ? }
然后就是發(fā)送NSNotification的地方了,在需要傳遞事件的地方執(zhí)行以下代碼即可:
[[NSNotificationCenter defaultCenter]postNotificationName:@"event-emited" object:nil userInfo:@{@"name":@"notification coming "}];
大功告成~~
PS:第一次寫,排版有點(diǎn)亂, 諒解下