2727
2828// 通知事件类型
2929#define NOTIFICATION_TYPE @" notificationType"
30+ #define NOTIFICATION_EVENT_TYPE @" notificationEventType"
3031#define NOTIFICATION_ARRIVED @" notificationArrived"
3132#define NOTIFICATION_OPENED @" notificationOpened"
3233#define NOTIFICATION_DISMISSED @" notificationDismissed"
@@ -51,7 +52,8 @@ @implementation RCTJPushModule
5152
5253RCT_EXPORT_MODULE (JPushModule);
5354
54- - (id )init {
55+ - (id )init
56+ {
5557 self = [super init ];
5658 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter ];
5759
@@ -95,7 +97,6 @@ - (id)init {
9597 return self;
9698}
9799
98-
99100RCT_EXPORT_METHOD (setDebugMode: (NSDictionary *)options)
100101{
101102 BOOL isDebug = false ;
@@ -107,6 +108,14 @@ - (id)init {
107108 }
108109}
109110
111+ RCT_EXPORT_METHOD (loadJS)
112+ {
113+ NSMutableArray *list = [RCTJPushEventQueue sharedInstance ]._notificationQueue ;
114+ if (list.count ) {
115+ [self sendApnsNotificationEventByDictionary: list[0 ]];
116+ }
117+ }
118+
110119RCT_EXPORT_METHOD (getRegisterId:(RCTResponseSenderBlock) callback)
111120{
112121 [JPUSHService registrationIDCompletionHandler: ^(int resCode, NSString *registrationID) {
@@ -116,7 +125,6 @@ - (id)init {
116125 }];
117126}
118127
119-
120128// tag
121129RCT_EXPORT_METHOD (addTags:(NSDictionary *)params)
122130{
@@ -298,13 +306,29 @@ - (void)sendConnectEvent:(NSNotification *)data {
298306 completion: NULL ];
299307}
300308
309+ // APNS通知消息
301310- (void )sendApnsNotificationEvent : (NSNotification *)data
302311{
303312 NSDictionary *responseData = [self convertApnsMessage: data];
304313 [self .bridge enqueueJSCall: @" RCTDeviceEventEmitter"
305314 method: @" emit"
306315 args: @[NOTIFICATION_EVENT, responseData]
307316 completion: NULL ];
317+ if ([RCTJPushEventQueue sharedInstance ]._notificationQueue .count ){
318+ [[RCTJPushEventQueue sharedInstance ]._notificationQueue removeAllObjects ];
319+ }
320+ }
321+
322+ - (void )sendApnsNotificationEventByDictionary : (NSDictionary *)data
323+ {
324+ NSDictionary *responseData = [self convertApnsMessage: data];
325+ [self .bridge enqueueJSCall: @" RCTDeviceEventEmitter"
326+ method: @" emit"
327+ args: @[NOTIFICATION_EVENT, responseData]
328+ completion: NULL ];
329+ if ([RCTJPushEventQueue sharedInstance ]._notificationQueue .count ){
330+ [[RCTJPushEventQueue sharedInstance ]._notificationQueue removeAllObjects ];
331+ }
308332}
309333
310334// 自定义消息
@@ -356,10 +380,18 @@ -(NSDictionary *)convertConnect:(NSNotification *)data {
356380 return responseData;
357381}
358382
359- -(NSDictionary *)convertApnsMessage : (NSNotification *)data {
360- NSNotificationName notificationName = data.name ;
361- NSDictionary *objectData = data.object ;
362- NSString *notificationType = ([notificationName isEqualToString: J_APNS_NOTIFICATION_OPENED_EVENT])?NOTIFICATION_OPENED:NOTIFICATION_ARRIVED;
383+ -(NSDictionary *)convertApnsMessage : (id )data
384+ {
385+ NSNotificationName notificationName;
386+ NSDictionary *objectData;
387+ if ([data isKindOfClass: [NSNotification class ]]){
388+ notificationName = [(NSNotification *)data name ];
389+ objectData = [(NSNotification *)data object ];
390+ }else if ([data isKindOfClass: [NSDictionary class ]]){
391+ notificationName = J_APNS_NOTIFICATION_OPENED_EVENT;
392+ objectData = data;
393+ }
394+ NSString *notificationEventType = ([notificationName isEqualToString: J_APNS_NOTIFICATION_OPENED_EVENT])?NOTIFICATION_OPENED:NOTIFICATION_ARRIVED;
363395 id alertData = objectData[@" aps" ][@" alert" ];
364396 NSString *title = @" " ;
365397 NSString *content = @" " ;
@@ -370,7 +402,6 @@ -(NSDictionary *)convertApnsMessage:(NSNotification *)data{
370402 title = alertData[@" title" ]?alertData[@" title" ]:@" " ;
371403 content = alertData[@" body" ]?alertData[@" body" ]:@" " ;
372404 }
373-
374405 NSDictionary *responseData;
375406 NSMutableDictionary * copyData = [[NSMutableDictionary alloc ] initWithDictionary: objectData];
376407 if (copyData[@" _j_business" ]) {
@@ -392,28 +423,33 @@ -(NSDictionary *)convertApnsMessage:(NSNotification *)data{
392423 NSString *value = [copyData objectForKey: key];
393424 [extrasData setObject: value forKey: key];
394425 };
395-
426+ NSString *messageID = objectData[ @" _j_msgid " ]?objectData[ @" _j_msgid " ]: @" " ;
396427 if (extrasData.count > 0 ) {
397- responseData = @{MESSAGE_ID:objectData[ @" _j_msgid " ] ,TITLE:title,CONTENT:content,EXTRAS:extrasData,NOTIFICATION_TYPE:notificationType };
428+ responseData = @{MESSAGE_ID:messageID ,TITLE:title,CONTENT:content,EXTRAS:extrasData,NOTIFICATION_EVENT_TYPE:notificationEventType };
398429 }
399430 else {
400- responseData = @{MESSAGE_ID:objectData[ @" _j_msgid " ] ,TITLE:title,CONTENT:content,NOTIFICATION_TYPE:notificationType };
431+ responseData = @{MESSAGE_ID:messageID ,TITLE:title,CONTENT:content,NOTIFICATION_EVENT_TYPE:notificationEventType };
401432 }
402433 return responseData;
403434}
404435
405- -(NSDictionary *)convertCustomMessage : (NSNotification *)data {
436+ -(NSDictionary *)convertCustomMessage : (NSNotification *)data
437+ {
406438 NSDictionary *objectData = data.object ;
407439 NSDictionary *responseData;
440+ NSString *messageID = objectData[@" _j_msgid" ]?objectData[@" _j_msgid" ]:@" " ;
441+ NSString *title = objectData[@" title" ]?objectData[@" title" ]:@" " ;
442+ NSString *content = objectData[@" content" ]?objectData[@" content" ]:@" " ;
408443 if (objectData[@" extras" ]){
409- responseData = @{MESSAGE_ID:objectData[ @" _j_msgid " ], CONTENT:objectData[ @" content" ] ,EXTRAS:objectData[@" extras" ]};
444+ responseData = @{MESSAGE_ID:messageID,TITLE:title, CONTENT:content,EXTRAS:objectData[@" extras" ]};
410445 }else {
411- responseData = @{MESSAGE_ID:objectData[ @" _j_msgid " ], CONTENT:objectData[ @" content" ] };
446+ responseData = @{MESSAGE_ID:messageID,TITLE:title, CONTENT:content};
412447 }
413448 return responseData;
414449}
415450
416- -(NSDictionary *)convertLocalMessage : (NSNotification *)data {
451+ -(NSDictionary *)convertLocalMessage : (NSNotification *)data
452+ {
417453 // NSLog(@"convertConnect 结果返回:%@", data);
418454 NSMutableDictionary *responseData = [[NSMutableDictionary alloc ] init ];
419455 return responseData;
0 commit comments