|
1 | | -#### 在 Native 中需要添加的代码 |
2 | | -- 在 AppDelegate.h 文件中 导入头文件 |
3 | | -``` |
4 | | -#import <RCTJPushModule.h> |
5 | | -#ifdef NSFoundationVersionNumber_iOS_9_x_Max |
6 | | -#import <UserNotifications/UserNotifications.h> |
7 | | -#endif |
8 | | -``` |
9 | | -- 在 AppDelegate.h 文件中 填写如下代码,这里的的 appkey、channel、和 isProduction 填写自己的 |
10 | | -``` |
11 | | -static NSString *appKey = @""; //填写appkey |
12 | | -static NSString *channel = @""; //填写channel 一般为nil |
13 | | -static BOOL isProduction = false; //填写isProdurion 平时测试时为false ,生产时填写true |
14 | | -``` |
15 | | -- 在AppDelegate.m 的didFinishLaunchingWithOptions 方法里面添加如下代码 |
16 | | -``` |
17 | | -- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
18 | | -{ |
19 | | - if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { |
20 | | - #ifdef NSFoundationVersionNumber_iOS_9_x_Max |
21 | | - JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; |
22 | | - entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; |
23 | | - [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; |
24 | | - |
25 | | -#endif |
26 | | -} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { |
27 | | - [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | |
28 | | - UIUserNotificationTypeSound | |
29 | | - UIUserNotificationTypeAlert) |
30 | | - categories:nil]; |
31 | | - } else { |
32 | | - [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | |
33 | | - UIRemoteNotificationTypeSound | |
34 | | - UIRemoteNotificationTypeAlert) |
35 | | - categories:nil]; |
36 | | - } |
37 | | - |
38 | | - [JPUSHService setupWithOption:launchOptions appKey:appKey |
39 | | - channel:channel apsForProduction:isProduction]; |
40 | | -} |
41 | | -``` |
42 | | -- 在AppDelegate.m 的didRegisterForRemoteNotificationsWithDeviceToken 方法中添加 [JPUSHService registerDeviceToken:deviceToken]; 如下所示 |
43 | | -``` |
44 | | -- (void)application:(UIApplication *)application |
45 | | -didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { |
46 | | - [JPUSHService registerDeviceToken:deviceToken]; |
47 | | -} |
48 | | -``` |
49 | | -- 为了在收到推送点击进入应用能够获取该条推送内容需要在 AppDelegate.m didReceiveRemoteNotification 方法里面添加 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo] 方法,注意:这里需要在两个方法里面加一个是iOS7以前的一个是iOS7即以后的,如果AppDelegate.m 没有这个两个方法则直接复制这两个方法,在 iOS10 的设备则可以使用JPush 提供的两个方法;如下所示 |
50 | | -``` |
51 | | -- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { |
52 | | - // 取得 APNs 标准信息内容 |
53 | | - |
54 | | - [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; |
55 | | -} |
56 | | -//iOS 7 Remote Notification |
57 | | -- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler { |
58 | | - |
59 | | - [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; |
60 | | -} |
61 | | -
|
62 | | -// iOS 10 Support |
63 | | -- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { |
64 | | - // Required |
65 | | - NSDictionary * userInfo = notification.request.content.userInfo; |
66 | | - if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { |
67 | | - [JPUSHService handleRemoteNotification:userInfo]; |
68 | | - [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; |
69 | | - } |
70 | | - completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置 |
71 | | -} |
72 | | -
|
73 | | -// iOS 10 Support |
74 | | -- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { |
75 | | - // Required |
76 | | - NSDictionary * userInfo = response.notification.request.content.userInfo; |
77 | | - if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { |
78 | | - [JPUSHService handleRemoteNotification:userInfo]; |
79 | | - [[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotification object:userInfo]; |
80 | | - } |
81 | | - completionHandler(); // 系统要求执行这个方法 |
82 | | -} |
83 | | -``` |
84 | | -然后在 js 代码里面通过如下回调获取通知 |
85 | | -``` |
86 | | -var { NativeAppEventEmitter } = require('react-native'); |
87 | | -
|
88 | | -var subscription = NativeAppEventEmitter.addListener( |
89 | | - 'ReceiveNotification', |
90 | | - (notification) => console.log(notification) |
91 | | -); |
92 | | -... |
93 | | -// 千万不要忘记忘记取消订阅, 通常在componentWillUnmount函数中实现。 |
94 | | -subscription.remove(); |
95 | | -``` |
96 | | - |
97 | | -在 jpush-react-native 1.2.9 开始提供 OpenNotification 事件。获取点击通知事件,需要获得该条推送需要在 js 代码加入如下监听代码:(注意这个事件只有在 iOS 10之后才有) |
98 | | -``` |
99 | | -var subscription = NativeAppEventEmitter.addListener( |
100 | | - 'OpenNotification', |
101 | | - (notification) => console.log(notification) |
102 | | -); |
103 | | -
|
104 | | -``` |
105 | | - |
106 | | - |
107 | | -- JPush 提供应用内消息,用户可以发送应用内消息给应用,如果手机应用在前台就会收到这个消息,否则存为离线消息。我们可以通过如下代码获取这个应用内消息 |
108 | | -``` |
109 | | -var { NativeAppEventEmitter } = require('react-native'); |
110 | | -
|
111 | | -var subscription = NativeAppEventEmitter.addListener( |
112 | | - 'networkDidReceiveMessage', |
113 | | - (message) => console.log(message) |
114 | | -); |
115 | | -... |
116 | | -// 千万不要忘记忘记取消订阅, 通常在componentWillUnmount函数中实现。 |
117 | | -subscription.remove(); |
118 | | -``` |
| 1 | +#### 在 Native 中需要添加的代码 |
| 2 | +- 在 AppDelegate.h 文件中 导入头文件 |
| 3 | +```objective-c |
| 4 | +#import <RCTJPushModule.h> |
| 5 | +#ifdef NSFoundationVersionNumber_iOS_9_x_Max |
| 6 | +#import <UserNotifications/UserNotifications.h> |
| 7 | +#endif |
| 8 | +``` |
| 9 | +- 在 AppDelegate.h 文件中 填写如下代码,这里的的 appkey、channel、和 isProduction 填写自己的 |
| 10 | +```objc |
| 11 | +static NSString *appKey = @"换成自己的 appkey"; //填写appkey |
| 12 | +static NSString *channel = @""; //填写channel 一般为nil |
| 13 | +static BOOL isProduction = false; //填写isProdurion 平时测试时为false ,生产时填写true |
| 14 | +``` |
| 15 | +- 在AppDelegate.m 的didFinishLaunchingWithOptions 方法里面添加如下代码 |
| 16 | +```objc |
| 17 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 18 | +{ |
| 19 | +JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; |
| 20 | + entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; |
| 21 | + [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; |
| 22 | + [JPUSHService setupWithOption:launchOptions appKey:appkey |
| 23 | + channel:nil apsForProduction:nil]; |
| 24 | +} |
| 25 | +``` |
| 26 | +- 在AppDelegate.m 的didRegisterForRemoteNotificationsWithDeviceToken 方法中添加 [JPUSHService registerDeviceToken:deviceToken]; 如下所示 |
| 27 | +```objective-c |
| 28 | +- (void)application:(UIApplication *)application |
| 29 | +didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { |
| 30 | + [JPUSHService registerDeviceToken:deviceToken]; |
| 31 | +} |
| 32 | +``` |
| 33 | +- 为了在收到推送点击进入应用能够获取该条推送内容需要在 AppDelegate.m didReceiveRemoteNotification 方法里面添加 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo] 方法,注意:这里需要在两个方法里面加一个是iOS7以前的一个是iOS7即以后的,如果AppDelegate.m 没有这个两个方法则直接复制这两个方法,在 iOS10 的设备则可以使用JPush 提供的两个方法;如下所示 |
| 34 | +```objective-c |
| 35 | +- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { |
| 36 | + // 取得 APNs 标准信息内容 |
| 37 | + |
| 38 | + [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; |
| 39 | +} |
| 40 | +//iOS 7 Remote Notification |
| 41 | +- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler { |
| 42 | + |
| 43 | + [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; |
| 44 | +} |
| 45 | + |
| 46 | +// iOS 10 Support |
| 47 | +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { |
| 48 | + // Required |
| 49 | + NSDictionary * userInfo = notification.request.content.userInfo; |
| 50 | + [JPUSHService handleRemoteNotification:userInfo]; |
| 51 | + [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo]; |
| 52 | + |
| 53 | + completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置 |
| 54 | +} |
| 55 | + |
| 56 | +// iOS 10 Support |
| 57 | +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { |
| 58 | + |
| 59 | + NSDictionary * userInfo = response.notification.request.content.userInfo; |
| 60 | + [JPUSHService handleRemoteNotification:userInfo]; |
| 61 | + [[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotification object:userInfo]; |
| 62 | + |
| 63 | + completionHandler(); |
| 64 | +} |
| 65 | +``` |
| 66 | +然后在 js 代码里面通过如下回调获取通知 |
| 67 | +``` |
| 68 | +var { NativeAppEventEmitter } = require('react-native'); |
| 69 | +
|
| 70 | +var subscription = NativeAppEventEmitter.addListener( |
| 71 | + 'ReceiveNotification', |
| 72 | + (notification) => console.log(notification) |
| 73 | +); |
| 74 | +... |
| 75 | +// 千万不要忘记忘记取消订阅, 通常在componentWillUnmount函数中实现。 |
| 76 | +subscription.remove(); |
| 77 | +``` |
| 78 | + |
| 79 | +在 jpush-react-native 1.2.9 开始提供 OpenNotification 事件。获取点击通知事件,需要获得该条推送需要在 js 代码加入如下监听代码:(注意这个事件只有在 iOS 10之后才有) |
| 80 | +``` |
| 81 | +var subscription = NativeAppEventEmitter.addListener( |
| 82 | + 'OpenNotification', |
| 83 | + (notification) => console.log(notification) |
| 84 | +); |
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +- JPush 提供应用内消息,用户可以发送应用内消息给应用,如果手机应用在前台就会收到这个消息,否则存为离线消息。我们可以通过如下代码获取这个应用内消息 |
| 90 | +``` |
| 91 | +var { NativeAppEventEmitter } = require('react-native'); |
| 92 | +
|
| 93 | +var subscription = NativeAppEventEmitter.addListener( |
| 94 | + 'networkDidReceiveMessage', |
| 95 | + (message) => console.log(message) |
| 96 | +); |
| 97 | +... |
| 98 | +// 千万不要忘记忘记取消订阅, 通常在componentWillUnmount函数中实现。 |
| 99 | +subscription.remove(); |
| 100 | +``` |
0 commit comments