iOS端 RCTEventEmitter 使用 Native發(fā)送通知消息到ReactNative

iOS 向ReactNative 發(fā)送通知事件

[self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder_BarcodeScanSuccessForRN"

的時(shí)候會(huì)提示

'sendDeviceEventWithName:body:'is deprecated: SubclassRCTEventEmitterinstead

查看源碼發(fā)現(xiàn),DeviceEventEmitterNativeAppEventEmitter 均已經(jīng)被遺棄。
DeviceEventEmitter源碼描述

/**
 * Deprecated - subclass NativeEventEmitter to create granular event modules instead of
 * adding all event listeners directly to RCTDeviceEventEmitter.
 */

NativeAppEventEmitter源碼描述

/**
 * Deprecated - subclass NativeEventEmitter to create granular event modules instead of
 * adding all event listeners directly to RCTNativeAppEventEmitter.
 */

同時(shí)都推薦使用NativeEventEmitter子類
在0.28版本之后,iOS向Js端發(fā)射消息的 思路如下
Natived端,新版本模塊類頭文件必須繼承自RCTEventEmitter
1.-(NSArray *)supportedEvents;中設(shè)置所支持的通知的名稱
2.- (void)startObserving; Native端開(kāi)啟通知
3.RN使用NativeEventEmitter.addListener模塊監(jiān)聽(tīng)通知
4.RN在 componentWillUnmount 中remove移除通知
5.- (void)stopObserving Native端移除通知

Native部分
.h

#import "RCTEventEmitter.h"

@interface MallManageAPI : RCTEventEmitter <RCTBridgeModule>

.m

-(NSArray *)supportedEvents {
return@[@"EventReminder_CollectionProductSuccessForRN"];
}
- (void)startObserving {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(CollectionProductSuccessForRNAction:)
                                                 name:@"CollectionProductSuccessForRN"
                                               object:nil];
  }

- (void)CollectionProductSuccessForRNAction:(NSNotification*)notification {
    
    dispatch_async(dispatch_get_main_queue(), ^{
    [self sendEventWithName:@"EventReminder_CollectionProductSuccessForRN" body:nil];
    });

}

- (void)stopObserving {
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"CollectionProductSuccessForRN"object:nil];
}

RN部分

import React, { Component } from 'react';
import {
    NativeModules,
    Platform,
    //NativeAppEventEmitter,
    NativeEventEmitter
} from 'react-native';
var NativeModulesByIOS = NativeModules.MallManageAPI;

const NativeNotificationMoudule = new NativeEventEmitter(NativeModulesByIOS)

 componentWillMount() {
        console.log('開(kāi)始訂閱通知...');
        if (Platform.OS === 'ios') {
            subscription = NativeNotificationMoudule.addListener(
                'EventReminder_CollectionProductSuccessForRN',
                (reminder) => {
                    this._onRefresh()
                }
            );
        } else {
        }
    }

componentWillUnmount() {
        if (Platform.OS === 'ios') {
            subscription.remove();
        } else {
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容