//MARK:开启通知
//JavaScript前端代码
//可以看到
//componentWillMount 中 先监听通知
//componentDidMount 中 再调用原生模块方法去发送通知
//componentWillUnmount 中 再去移除监听
//componentDidMount() {
// nativeAppEv= NativeAppEventEmitter.addListener(
// 'notifyctrlCar',
// (reminder) => console.log(1111118888899999)
// );
//等 RN组件 监听事件通知后 在开启通知
// CalendarManager.startObserving('notifyctrlCar');
//}
//componentWillUnmount() {
// nativeAppEv.remove();
//}
//iOS前端代码
RCT_EXPORT_METHOD(startObserving:(NSString *)name){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OCsendToReactNative:) name:name object:nil];
}
-(void)OCsendToReactNative:(NSNotification *)notification
{
[self.bridge.eventDispatcher sendAppEventWithName:@"notifyctrlCar"
body:notification.name];
}
+(void)OCsendMessageToReactNative:(NSDictionary *)dictionary {
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyctrlCar" object:nil userInfo:dictionary];
}
//只是为了在RN中调用测试而已,iOS中直接发[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyctrlCar" object:nil];
RCT_EXPORT_METHOD(postNotificationName){
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyctrlCar" object:nil];
}
网友评论