一、正常步骤,用户操作原生并告知RN
项目要用到原生收到推送时,给JS发送通知,
首先找了这篇16年文章 : http://blog.csdn.net/pz789as/article/details/52837853,参照前辈描述,很详尽
继承了RCTEventEmitter这个类,用来发送通知的:
屏幕快照 2018-01-26 下午3.18.53.png
而后实现这两个方法,
实现两个方法.png
二、然后就是坑了:bridge is not set.
当用户操作触发时调用sendEventWithName这个方法时,就会出现下面这个问题了:
问题.png
三、解决方案
使用单例解决这个问题:
+ (id)allocWithZone:(NSZone *)zone {
static XGPushManager *pushManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
pushManager = [super allocWithZone:zone];
});
return pushManager;
}
四、原生被动接收通知时,再通知RN
当推送来时,只能再AppDelegate中拿到,这时候创建的这个类并不知道这个事件,因此要先给这个类注册通知:
注册通知.png
+ (id)allocWithZone:(NSZone *)zone {
static XGPushManager *pushManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
pushManager = [super allocWithZone:zone];
[[NSNotificationCenter defaultCenter] addObserver:pushManager selector:@selector(receivePushNotificateRN:) name:@"EventReminder" object:nil];
});
return pushManager;
}
而后在需要的地方发送该通知就好了,这时候,RN就同样的能接收到了。
(注意发送名字和在工具类中注册的名字一样。 可以与通知RN的名字不一样)
[[NSNotificationCenter defaultCenter] postNotificationName:@"EventReminder" object:@{@"1":@"2"}];
参考文章:
1、https://www.v2ex.com/t/424276#r_5232907
2、//www.greatytc.com/p/8bc6d699b471