• 原生和RN之间通知流程


    一,RN发送通知给原生;

    分三个步骤:

    1.创建桥接类;2.iOS项目下定义方法;3.RN项目下使用定义的方法。

    具体例子如下:

    1.创建桥接类

    IOSCommonModule : NSObject<RCTBridgeModule>

    // 在该类.m文件导出模块

    RCT_EXPORT_MODULE();

    2.该类下定义RN调用的方法openScanPage,callback里面是返回给RN用的数据(可以不带这个参数看业务需求)

    #pragma mark - 跳转扫码页面

    RCT_EXPORT_METHOD(openScanPage:(RCTResponseSenderBlock)callback)

    {

       dispatch_async(dispatch_get_main_queue(), ^{

         

         ScanViewController *vc = [ScanViewController new];

         vc.resultBlock = ^(NSString* result) {

           NSLog(@"++++++%@",result);

           callback(@[result]);

         };

         AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);

         [(UINavigationController*)delegate.window.rootViewController pushViewController:vc animated:NO];

       });

    }

    3.RN项目中调用方法openScanPage

    NativeModules.IOSCommonModule.openScanPage((content) => {

        //使用content 

    });

    二,原生发送通知给RN;

    分四个步骤:

    1.注册支持的事件;2.根据业务逻辑处理写本地通知(监听/发送/移除通知三部走);3.发送事件;4.RN进行监听。

    具体例子如下:

    EventEmitterManager在该类下面实现:

    RN_UdpNotification通知用于通知RN(使用supportedEvents和sendEventWithName)

    #define RN_UdpNotification @"UdpNotification" 

    IOS_UDP_NOTIRNACTION通知用于本地通知(使用本地通知的方法)

    #define IOS_UDP_NOTIRNACTION @"IOS_UDPNotificationForRN"

    1.注册支持的事件

    - (NSArray<NSString *> *)supportedEvents

    {

      return @[RN_UdpNotification];

    }

    2.根据业务逻辑处理写本地通知(监听/发送/移除通知三部走)+ 发送通知给RN(sendEventWithName)

    //监听本地通知

    - (void)startObserving {

      hasListeners = YES;

      [[NSNotificationCenter defaultCenter] addObserver:self

                                               selector:@selector(UDPForRNAction:)

                                                   name:IOS_UDP_NOTIRNACTION

                                                 object:nil];

    }

     //发送本地通知

    -(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error

    {

      

      if (tag == 200) {

        [[NSNotificationCenter defaultCenter]postNotificationName:IOS_UDP_NOTIRNACTION object:nil userInfo:@{@"udpAction":@"send",

                                                                                                     @"error":[NSString stringWithFormat:@"%ld",(long)error.code]}];

      }

    }

    //发送通知给RN(sendEventWithName方法)

    - (void)UDPForRNAction:(NSNotification*)notification {

      if ([(NSString*)[notification.userInfo objectForKey:@"udpAction"] isEqualToString:@"send"]) {

        [self sendEventWithName:RN_UdpNotification body:notification.userInfo];

        

      }else if ([(NSString*)[notification.userInfo objectForKey:@"udpAction"] isEqualToString:@"receive"]) {

        [self sendEventWithName:RN_UdpNotification body:notification.userInfo];

      }

      

    }

    //移除通知

    - (void)stopObserving {

      hasListeners = NO;  

      [[NSNotificationCenter defaultCenter]removeObserver:self];

    }

    3.RN项目下监听和实现

    const UDPNOTIFICATION = 'UdpNotification'
    const {EventEmitterManager} = NativeModules;
    const udpEventEmitterManager = new NativeEventEmitter(EventEmitterManager);

    this.udpSubscription = udpEventEmitterManager.addListener(
    UDPNOTIFICATION,
    (receiveData) => {



    // Global.showToast(JSON.stringify(receiveData))
    // console.warn('接收到消息了'+receiveData);

    if (PlatformUtil.isAndroid()) {
    this.setState({datas: receiveData})
    } else {
    let action = receiveData.udpAction;
    let error = receiveData.error;
    let udpData = receiveData.data;

    // Global.showToast('接收到消息了'+JSON.stringify(receiveData)+'///'+JSON.stringify(action)+'///'+JSON.stringify(error)+'///'+JSON.stringify(udpData))
    // console.log('接收到消息了'+JSON.stringify(receiveData)+'///'+JSON.stringify(action)+'///'+JSON.stringify(error)+'///'+JSON.stringify(udpData))


    if (action=='send') {
    //发送失败
    // Global.showToast('接收到消息了'+JSON.stringify(error))

    //重新发送
    //注销
    NativeModules.IOSCommonModule.cancelUDP('8800', () => {
    })
    //初始化
    NativeModules.IOSCommonModule.initUDPWithPort('0', (msg) => {
    console.warn(msg)
    })
    //发送UDP
    NativeModules.IOSCommonModule.sendUDPMessage('8800', this.wifiName, this.wifiPassword, (msg) => {
    console.warn(msg)
    })


    } else if (action=='receive') {
    //发送成功
    // Global.showToast('接收到消息了'+JSON.stringify(udpData))

    this.state.datas.push(udpData)
    this.setState({datas:this.state.datas})
    }
    }


    });

  • 相关阅读:
    Node.js模块风格在浏览器中的尝试
    Node.js的模块写法入门
    JavaScript中“+”的陷阱(续)
    Firefox/Chrome/Safari的中可直接使用$/$$函数进行调试
    使用r.js压缩整个项目的JavaScript文件
    拥抱模块化的JavaScript
    图片播放(3)
    JavaScript中“+”的陷阱
    仅IE6/7浏览器SPAN元素包含块级元素会使SPAN的背景色显示
    JavaScript模态对话框类(拖拽时动画)
  • 原文地址:https://www.cnblogs.com/superCode-7/p/14145399.html
Copyright © 2020-2023  润新知