Skip to content

Commit 6adc582

Browse files
author
詹强
committed
iOS- 支持应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
1 parent 5242052 commit 6adc582

File tree

5 files changed

+106
-18
lines changed

5 files changed

+106
-18
lines changed

example/ios/example/AppDelegate.m

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2323
// JPush初始化配置
2424
[JPUSHService setupWithOption:launchOptions appKey:@"129c21dc4cb5e6f6de194003"
2525
channel:@"dev" apsForProduction:YES];
26-
// Apns
26+
// APNS
2727
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
2828
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
2929
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
@@ -51,15 +51,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5151

5252
//************************************************JPush start************************************************
5353

54-
//注册 APNs 成功并上报 DeviceToken
54+
//注册 APNS 成功并上报 DeviceToken
5555
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
5656
[JPUSHService registerDeviceToken:deviceToken];
5757
}
5858

59-
//iOS 7 Apns
59+
//iOS 7 APNS
6060
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
6161
// iOS 10 以下 Required
62-
NSLog(@"iOS 7 Apns");
62+
NSLog(@"iOS 7 APNS");
6363
[JPUSHService handleRemoteNotification:userInfo];
6464
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
6565
completionHandler(UIBackgroundFetchResultNewData);
@@ -77,7 +77,7 @@ - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentN
7777
NSDictionary * userInfo = notification.request.content.userInfo;
7878
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
7979
// Apns
80-
NSLog(@"iOS 10 Apns 前台收到消息");
80+
NSLog(@"iOS 10 APNS 前台收到消息");
8181
[JPUSHService handleRemoteNotification:userInfo];
8282
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
8383
}
@@ -93,17 +93,20 @@ - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentN
9393
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)())completionHandler {
9494
NSDictionary * userInfo = response.notification.request.content.userInfo;
9595
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
96-
NSLog(@"iOS 10 Apns 消息事件回调");
96+
NSLog(@"iOS 10 APNS 消息事件回调");
9797
// Apns
9898
[JPUSHService handleRemoteNotification:userInfo];
99+
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
100+
[[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
99101
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
100102
}
101103
else {
102104
// 本地通知 todo
103105
NSLog(@"iOS 10 本地通知 消息事件回调");
104106
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_EVENT object:userInfo];
105107
}
106-
completionHandler(); // 系统要求执行这个方法
108+
// 系统要求执行这个方法
109+
completionHandler();
107110
}
108111

109112
//自定义消息
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// RCTJPushEventQueue.h
3+
// DoubleConversion
4+
//
5+
// Created by wicked on 2019/9/26.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
10+
11+
@interface RCTJPushEventQueue : NSObject
12+
13+
+ (nonnull instancetype) sharedInstance;
14+
15+
@property NSMutableArray<NSDictionary *> *_notificationQueue;;
16+
17+
@end
18+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// RCTJPushEventQueue.m
3+
// DoubleConversion
4+
//
5+
// Created by wicked on 2019/9/26.
6+
//
7+
8+
#import "RCTJPushEventQueue.h"
9+
10+
@implementation RCTJPushEventQueue
11+
12+
+ (instancetype)sharedInstance {
13+
static RCTJPushEventQueue *instance = nil;
14+
static dispatch_once_t onceToken;
15+
dispatch_once(&onceToken, ^{
16+
instance = [[self alloc] init];
17+
});
18+
return instance;
19+
}
20+
21+
- (instancetype)init
22+
{
23+
self = [super init];
24+
if (self) {
25+
self._notificationQueue = [NSMutableArray new];
26+
}
27+
return self;
28+
}
29+
30+
31+
@end

ios/RCTJPushModule/RCTJPushModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif
1212

1313
#import "JPUSHService.h"
14+
#import "RCTJPushEventQueue.h"
1415

1516
#define J_APNS_NOTIFICATION_ARRIVED_EVENT @"J_APNS_NOTIFICATION_ARRIVED_EVENT"
1617
#define J_APNS_NOTIFICATION_OPENED_EVENT @"J_APNS_NOTIFICATION_OPENED_EVENT"

ios/RCTJPushModule/RCTJPushModule.m

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
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,9 @@ @implementation RCTJPushModule
5152

5253
RCT_EXPORT_MODULE(JPushModule);
5354

54-
- (id)init {
55+
- (id)init
56+
{
57+
NSLog(@"xxx init");
5558
self = [super init];
5659
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
5760

@@ -95,7 +98,6 @@ - (id)init {
9598
return self;
9699
}
97100

98-
99101
RCT_EXPORT_METHOD(setDebugMode: (NSDictionary *)options)
100102
{
101103
BOOL isDebug = false;
@@ -107,6 +109,14 @@ - (id)init {
107109
}
108110
}
109111

112+
RCT_EXPORT_METHOD(loadJS)
113+
{
114+
NSMutableArray *list = [RCTJPushEventQueue sharedInstance]._notificationQueue;
115+
if(list.count) {
116+
[self sendApnsNotificationEventByDictionary:list[0]];
117+
}
118+
}
119+
110120
RCT_EXPORT_METHOD(getRegisterId:(RCTResponseSenderBlock) callback)
111121
{
112122
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
@@ -116,7 +126,6 @@ - (id)init {
116126
}];
117127
}
118128

119-
120129
//tag
121130
RCT_EXPORT_METHOD(addTags:(NSDictionary *)params)
122131
{
@@ -298,13 +307,29 @@ - (void)sendConnectEvent:(NSNotification *)data {
298307
completion:NULL];
299308
}
300309

310+
//APNS通知消息
301311
- (void)sendApnsNotificationEvent:(NSNotification *)data
302312
{
303313
NSDictionary *responseData = [self convertApnsMessage:data];
304314
[self.bridge enqueueJSCall:@"RCTDeviceEventEmitter"
305315
method:@"emit"
306316
args:@[NOTIFICATION_EVENT, responseData]
307317
completion:NULL];
318+
if([RCTJPushEventQueue sharedInstance]._notificationQueue.count){
319+
[[RCTJPushEventQueue sharedInstance]._notificationQueue removeAllObjects];
320+
}
321+
}
322+
323+
- (void)sendApnsNotificationEventByDictionary:(NSDictionary *)data
324+
{
325+
NSDictionary *responseData = [self convertApnsMessage:data];
326+
[self.bridge enqueueJSCall:@"RCTDeviceEventEmitter"
327+
method:@"emit"
328+
args:@[NOTIFICATION_EVENT, responseData]
329+
completion:NULL];
330+
if([RCTJPushEventQueue sharedInstance]._notificationQueue.count){
331+
[[RCTJPushEventQueue sharedInstance]._notificationQueue removeAllObjects];
332+
}
308333
}
309334

310335
//自定义消息
@@ -356,10 +381,18 @@ -(NSDictionary *)convertConnect:(NSNotification *)data {
356381
return responseData;
357382
}
358383

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;
384+
-(NSDictionary *)convertApnsMessage:(id)data
385+
{
386+
NSNotificationName notificationName;
387+
NSDictionary *objectData;
388+
if([data isKindOfClass:[NSNotification class]]){
389+
notificationName = [(NSNotification *)data name];
390+
objectData = [(NSNotification *)data object];
391+
}else if([data isKindOfClass:[NSDictionary class]]){
392+
notificationName = J_APNS_NOTIFICATION_OPENED_EVENT;
393+
objectData = data;
394+
}
395+
NSString *notificationEventType = ([notificationName isEqualToString:J_APNS_NOTIFICATION_OPENED_EVENT])?NOTIFICATION_OPENED:NOTIFICATION_ARRIVED;
363396
id alertData = objectData[@"aps"][@"alert"];
364397
NSString *title = @"";
365398
NSString *content = @"";
@@ -394,15 +427,16 @@ -(NSDictionary *)convertApnsMessage:(NSNotification *)data{
394427
};
395428

396429
if (extrasData.count > 0) {
397-
responseData = @{MESSAGE_ID:objectData[@"_j_msgid"],TITLE:title,CONTENT:content,EXTRAS:extrasData,NOTIFICATION_TYPE:notificationType};
430+
responseData = @{MESSAGE_ID:objectData[@"_j_msgid"],TITLE:title,CONTENT:content,EXTRAS:extrasData,NOTIFICATION_EVENT_TYPE:notificationEventType};
398431
}
399432
else {
400-
responseData = @{MESSAGE_ID:objectData[@"_j_msgid"],TITLE:title,CONTENT:content,NOTIFICATION_TYPE:notificationType};
433+
responseData = @{MESSAGE_ID:objectData[@"_j_msgid"],TITLE:title,CONTENT:content,NOTIFICATION_EVENT_TYPE:notificationEventType};
401434
}
402435
return responseData;
403436
}
404437

405-
-(NSDictionary *)convertCustomMessage:(NSNotification *)data {
438+
-(NSDictionary *)convertCustomMessage:(NSNotification *)data
439+
{
406440
NSDictionary *objectData = data.object;
407441
NSDictionary *responseData;
408442
if(objectData[@"extras"]){
@@ -413,7 +447,8 @@ -(NSDictionary *)convertCustomMessage:(NSNotification *)data {
413447
return responseData;
414448
}
415449

416-
-(NSDictionary *)convertLocalMessage:(NSNotification *)data {
450+
-(NSDictionary *)convertLocalMessage:(NSNotification *)data
451+
{
417452
//NSLog(@"convertConnect 结果返回:%@", data);
418453
NSMutableDictionary *responseData = [[NSMutableDictionary alloc] init];
419454
return responseData;

0 commit comments

Comments
 (0)