Skip to content

Commit 189e6e7

Browse files
Merge pull request #490 from jpush/dev
Dev
2 parents 1062ce8 + de75e52 commit 189e6e7

File tree

4 files changed

+111
-32
lines changed

4 files changed

+111
-32
lines changed

documents/api.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [setBadge](#setbadge)
2020
* [getBadge](#getbadge)
2121
* [setLocalNotification](#setlocalnotification)
22+
* [getLaunchAppNotification](#getlaunchappnotification)
2223
* [点击推送启动应用事件](#open-notification-launch-app-event)
2324
* [网络成功登陆事件](#network-did-login-event)
2425
* [Android Only API](#android-only-api)
@@ -292,6 +293,22 @@ JPushModule.setLocalNotification(
292293
)
293294
```
294295

296+
#### getLaunchAppNotification
297+
298+
点击推送启动应用的时候原生会将该 notification 缓存起来,该方法用于获取缓存的 notification。
299+
300+
```javascript
301+
JPushModule.getLaunchAppNotification( notification => {
302+
if (notification === undefined) {
303+
// 说明应用不是通过点击通知启动的,是通过点击应用 icon
304+
} else if (notification['aps'] === undefined) {
305+
// 说明是 local notification
306+
} else {
307+
// 说明是 remote notification
308+
}
309+
})
310+
```
311+
295312
#### 点击推送启动应用事件
296313

297314
**NOTE**: iOS 需要安装到 [email protected]+ 。

documents/api_en.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [setBadge](#setbadge)
2020
* [getBadge](#getbadge)
2121
* [setLocalNotification](#setlocalnotification)
22+
* [getLaunchAppNotification](#getlaunchappnotification)
2223
* [Open Notification Launch App Event](#open-notification-launch-app-event)
2324
* [Network Did Login Event](#network-did-login-event)
2425
* [Android Only API](#android-only-api)
@@ -263,6 +264,22 @@ JPushModule.setLocalNotification(
263264
)
264265
```
265266

267+
#### getLaunchAppNotification
268+
269+
Get Launch app's notification, this function will get the launch notificatiton from cache .
270+
271+
```javascript
272+
JPushModule.getLaunchAppNotification( notification => {
273+
if (notification === undefined) {
274+
// application was launch by tap app's icon
275+
} else if (notification['aps'] === undefined) {
276+
// application was launch by tap local notification
277+
} else {
278+
// application was launch by tap remote notification
279+
}
280+
})
281+
```
282+
266283
#### Open Notification Launch App Event
267284

268285
**NOTE**: iOS need update to [email protected]+

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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jpush-react-native",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "a jpush plugin for react native application",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)