Skip to content

Commit 8b6d1df

Browse files
committed
iOS:update move remotenotification extras key-value to extras field just like android
1 parent 1ebb7cc commit 8b6d1df

File tree

1 file changed

+76
-31
lines changed

1 file changed

+76
-31
lines changed

ios/RCTJPushModule/RCTJPushModule.m

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ - (id)init {
101101
name:kJPFOpenNotificationToLaunchApp
102102
object:nil];
103103

104-
if ([RCTJPushActionQueue sharedInstance].openedLocalNotification != nil) {
105-
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotificationToLaunchApp object:[RCTJPushActionQueue sharedInstance].openedLocalNotification];
106-
}
104+
// if ([RCTJPushActionQueue sharedInstance].openedLocalNotification != nil) {
105+
// [self alert:@"openedLocalNotification is not nil"];
106+
// [[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotificationToLaunchApp object:[RCTJPushActionQueue sharedInstance].openedLocalNotification];
107+
// }
107108

108109
return self;
109110
}
@@ -125,13 +126,16 @@ - (void)reactJSDidload {
125126
} else {
126127
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFNetworkDidCloseNotification object:nil];
127128
}
128-
129129
}
130130

131131
- (void)setBridge:(RCTBridge *)bridge {
132132
_bridge = bridge;
133133
[RCTJPushActionQueue sharedInstance].openedRemoteNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
134-
[RCTJPushActionQueue sharedInstance].openedLocalNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
134+
if ([_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]) {
135+
UILocalNotification *localNotification = [_bridge.launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
136+
[RCTJPushActionQueue sharedInstance].openedLocalNotification = localNotification.userInfo;// null?
137+
}
138+
135139
}
136140

137141
// request push notification permissions only
@@ -146,34 +150,89 @@ - (void)setBridge:(RCTBridge *)bridge {
146150
}
147151

148152
RCT_EXPORT_METHOD(getLaunchAppNotification:(RCTResponseSenderBlock)callback) {
149-
153+
NSDictionary *notification;
150154
if ([RCTJPushActionQueue sharedInstance].openedRemoteNotification != nil) {
151-
callback(@[[RCTJPushActionQueue sharedInstance].openedRemoteNotification]);
155+
notification = [self jpushFormatNotification: [RCTJPushActionQueue sharedInstance].openedRemoteNotification];
156+
callback(@[notification]);
152157
return;
153158
}
154-
159+
155160
if ([RCTJPushActionQueue sharedInstance].openedLocalNotification != nil) {
156-
callback(@[[RCTJPushActionQueue sharedInstance].openedLocalNotification]);
161+
notification = [self jpushFormatNotification:[RCTJPushActionQueue sharedInstance].openedLocalNotification];
162+
callback(@[notification]);
157163
return;
158164
}
159-
165+
160166
callback(@[]);
161167
}
162168

163169
RCT_EXPORT_METHOD(getApplicationIconBadge:(RCTResponseSenderBlock)callback) {
164170
callback(@[@([UIApplication sharedApplication].applicationIconBadgeNumber)]);
165171
}
166172

173+
// TODO:
167174
- (void)openNotificationToLaunchApp:(NSNotification *)notification {
168-
id obj = [notification object];
169-
[self.bridge.eventDispatcher sendAppEventWithName:@"openNotificationLaunchApp" body:obj];
175+
NSDictionary *obj = [notification object];
176+
[self.bridge.eventDispatcher sendAppEventWithName:@"openNotificationLaunchApp" body:[self jpushFormatNotification:obj]];
170177
}
171178

179+
// TODO:
172180
- (void)openNotification:(NSNotification *)notification {
173-
id obj = [notification object];
174-
[self.bridge.eventDispatcher sendAppEventWithName:@"openNotification" body:obj];
181+
NSDictionary *obj = [notification object];
182+
[self.bridge.eventDispatcher sendAppEventWithName:@"openNotification" body: [self jpushFormatNotification:obj]];
183+
}
184+
185+
- (NSMutableDictionary *)jpushFormatNotification:(NSDictionary *)dic {
186+
if (!dic) {
187+
return @[].mutableCopy;
188+
}
189+
if (dic.count == 0) {
190+
return @[].mutableCopy;
191+
}
192+
193+
if (dic[@"aps"]) {
194+
return [self jpushFormatAPNSDic:dic];
195+
} else {
196+
return [self jpushFormatLocalNotificationDic:dic];
197+
}
175198
}
176199

200+
- (NSMutableDictionary *)jpushFormatLocalNotificationDic:(NSDictionary *)dic {
201+
return [NSMutableDictionary dictionaryWithDictionary:dic];
202+
}
203+
204+
- (NSMutableDictionary *)jpushFormatAPNSDic:(NSDictionary *)dic {
205+
NSMutableDictionary *extras = @{}.mutableCopy;
206+
for (NSString *key in dic) {
207+
if([key isEqualToString:@"_j_business"] ||
208+
[key isEqualToString:@"_j_msgid"] ||
209+
[key isEqualToString:@"_j_uid"] ||
210+
[key isEqualToString:@"actionIdentifier"] ||
211+
[key isEqualToString:@"aps"]) {
212+
continue;
213+
}
214+
// 和 android 统一将 extras 字段移动到 extras 里面
215+
extras[key] = dic[key];
216+
}
217+
NSMutableDictionary *formatDic = dic.mutableCopy;
218+
formatDic[@"extras"] = extras;
219+
220+
// 新增 应用状态
221+
switch ([UIApplication sharedApplication].applicationState) {
222+
case UIApplicationStateInactive:
223+
formatDic[@"appState"] = @"inactive";
224+
break;
225+
case UIApplicationStateActive:
226+
formatDic[@"appState"] = @"active";
227+
break;
228+
case UIApplicationStateBackground:
229+
formatDic[@"appState"] = @"background";
230+
break;
231+
default:
232+
break;
233+
}
234+
return formatDic;
235+
}
177236

178237
- (void)networkConnecting:(NSNotification *)notification {
179238
_isJPushDidLogin = false;
@@ -215,27 +274,13 @@ - (void)networkDidReceiveMessage:(NSNotification *)notification {
215274
body:[notification userInfo]];
216275
}
217276

277+
278+
// TODO:
218279
- (void)receiveRemoteNotification:(NSNotification *)notification {
219280

220281
if ([RCTJPushActionQueue sharedInstance].isReactDidLoad == YES) {
221282
NSDictionary *obj = [notification object];
222-
NSMutableDictionary *notificationDic = [[NSMutableDictionary alloc] initWithDictionary:obj];
223-
224-
switch ([UIApplication sharedApplication].applicationState) {
225-
case UIApplicationStateInactive:
226-
notificationDic[@"appState"] = @"inactive";
227-
break;
228-
case UIApplicationStateActive:
229-
notificationDic[@"appState"] = @"active";
230-
break;
231-
case UIApplicationStateBackground:
232-
notificationDic[@"appState"] = @"background";
233-
break;
234-
default:
235-
break;
236-
}
237-
NSDictionary *event = [NSDictionary dictionaryWithDictionary: notificationDic];
238-
[self.bridge.eventDispatcher sendAppEventWithName:@"receiveNotification" body:event];
283+
[self.bridge.eventDispatcher sendAppEventWithName:@"receiveNotification" body: [self jpushFormatNotification:obj]];
239284
} else {
240285
[[RCTJPushActionQueue sharedInstance] postNotification:notification];
241286
}

0 commit comments

Comments
 (0)