Skip to content

Commit 9d31558

Browse files
committed
add getLaunchAppNotification api for iOS
1 parent 2533a67 commit 9d31558

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

example/ios/PushDemo/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3030

3131
// jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
3232

33-
jsCodeLocation = [NSURL URLWithString:@"http://192.168.10.155:8081/index.bundle?platform=ios&dev=true"];
33+
jsCodeLocation = [NSURL URLWithString:@"http://192.168.9.118:8081/index.bundle?platform=ios&dev=true"];
3434
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
3535
moduleName:@"PushDemo"
3636
initialProperties:nil

index.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,19 @@ type JResultCallback<T = undefined> = (result: T) => void
1212

1313
declare class JPush {
1414
/**
15-
* Android only
1615
* 初始化JPush 必须先初始化才能执行其他操作
1716
*/
1817
static initPush(): void
1918

2019
static stopPush(): void
2120

22-
/**
23-
* Android Only
24-
*/
2521
static resumePush(): void
2622

2723
/**
2824
* Android Only
2925
*/
3026
static notifyJSDidLoad(cb: JResultCallback<number>): void
3127

32-
/**
33-
* Android Only
34-
*/
3528
static clearAllNotifications(): void
3629

3730
/**
@@ -167,6 +160,11 @@ declare class JPush {
167160
*/
168161
static removeReceiveCustomMsgListener(cb: JSuccessCallback<any>): void
169162

163+
/**
164+
* iOS Onlylalala
165+
*/
166+
static getLaunchAppNotification (cb: JSuccessCallback<string>): void
167+
170168
/**
171169
* iOS Only
172170
*/

index.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { DeviceEventEmitter, NativeModules } from 'react-native'
1+
import {
2+
DeviceEventEmitter,
3+
NativeModules,
4+
Platform
5+
} from 'react-native'
26

37
const JPushModule = NativeModules.JPushModule
48

@@ -15,22 +19,31 @@ const receiveExtrasEvent = 'receiveExtras' // Android Only
1519

1620
export default class JPush {
1721
/**
18-
* Android Only
1922
* 初始化JPush 必须先初始化才能执行其他操作
2023
*/
2124
static initPush () {
22-
JPushModule.initPush()
25+
if (Platform.OS == "android") {
26+
JPushModule.initPush()
27+
} else {
28+
JPush.setupPush()
29+
}
2330
}
24-
31+
/**
32+
* 停止推送,调用该方法后将不再受到推送
33+
*/
2534
static stopPush () {
2635
JPushModule.stopPush()
2736
}
2837

2938
/**
30-
* Android Only
39+
* 恢复推送功能,停止推送后,可调用该方法重新获得推送能力
3140
*/
3241
static resumePush () {
33-
JPushModule.resumePush()
42+
if (Platform.OS == "android") {
43+
JPushModule.resumePush()
44+
} else {
45+
JPush.setupPush()
46+
}
3447
}
3548

3649
/**
@@ -59,10 +72,14 @@ export default class JPush {
5972
}
6073

6174
/**
62-
* Android Only
75+
* 清除通知栏的所有通知
6376
*/
6477
static clearAllNotifications () {
65-
JPushModule.clearAllNotifications()
78+
if (Platform.OS == "android") {
79+
JPushModule.clearAllNotifications()
80+
} else {
81+
JPush.setBadge(0,() => {})
82+
}
6683
}
6784

6885
/**
@@ -305,9 +322,22 @@ export default class JPush {
305322
listeners[cb] = null
306323
}
307324

325+
/**
326+
* iOS Only
327+
* 点击推送启动应用的时候原生会将该 notification 缓存起来,该方法用于获取缓存 notification
328+
* 注意:notification 可能是 remoteNotification 和 localNotification,两种推送字段不一样。
329+
* 如果不是通过点击推送启动应用,比如点击应用 icon 直接启动应用,notification 会返回 undefine。
330+
* @param {Function} cb = (notification) => {}
331+
*/
332+
static getLaunchAppNotification (cb) {
333+
JPushModule.getLaunchAppNotification(cb)
334+
}
335+
308336
/**
309337
* iOS Only
310338
* 监听:应用没有启动的状态点击推送打开应用
339+
* 注意:2.2.0 版本开始,提供了 getLaunchAppNotification
340+
*
311341
* @param {Function} cb = (notification) => {}
312342
*/
313343
static addOpenNotificationLaunchAppListener (cb) {
@@ -320,6 +350,7 @@ export default class JPush {
320350
}
321351

322352
/**
353+
* @deprecated Since version 2.2.0, will deleted in 3.0.0.
323354
* iOS Only
324355
* 取消监听:应用没有启动的状态点击推送打开应用
325356
* @param {Function} cb = () => {}

ios/RCTJPushModule/RCTJPushModule.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ - (void)setBridge:(RCTBridge *)bridge {
145145
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
146146
}
147147

148+
RCT_EXPORT_METHOD(getLaunchAppNotification:(RCTResponseSenderBlock)callback) {
149+
150+
if ([RCTJPushActionQueue sharedInstance].openedRemoteNotification != nil) {
151+
callback(@[[RCTJPushActionQueue sharedInstance].openedRemoteNotification]);
152+
return;
153+
}
154+
155+
if ([RCTJPushActionQueue sharedInstance].openedLocalNotification != nil) {
156+
callback(@[[RCTJPushActionQueue sharedInstance].openedLocalNotification]);
157+
return;
158+
}
159+
160+
callback(@[]);
161+
}
162+
148163
RCT_EXPORT_METHOD(getApplicationIconBadge:(RCTResponseSenderBlock)callback) {
149164
callback(@[@([UIApplication sharedApplication].applicationIconBadgeNumber)]);
150165
}

0 commit comments

Comments
 (0)