-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.plugin.js
389 lines (369 loc) · 14.4 KB
/
app.plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
const {
AndroidConfig,
withAppBuildGradle,
withSettingsGradle,
withAndroidManifest,
withAppDelegate,
withPodfile,
withInfoPlist,
} = require('@expo/config-plugins');
let JPUSH_APPKEY = 'appKey',
JPUSH_CHANNEL = 'channel';
const withJPush = (config, props) => {
if (!props || !props.appKey || !props.channel)
throw new Error('[MX_JPush_Expo] 请传入参数 appKey & channel');
JPUSH_APPKEY = props.appKey;
JPUSH_CHANNEL = props.channel;
config = setInfoPList(config);
config = setInterface(config);
config = setAppDelegate(config);
config = setAndroidManifest(config);
config = setAppBuildGradle(config);
config = setSettingsGradle(config);
config = setPodfilePostInstall(config);
return config;
};
const setInfoPList = config =>
withInfoPlist(config, config => {
config.modResults.UIBackgroundModes = ['fetch', 'remote-notification'];
return config;
});
const setPodfilePostInstall = config =>
withPodfile(config, config => {
const postInstallScript = `
installer.pods_project.build_configurations.each do |config|
config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
end
`;
const installScript = 'post_install do |installer|';
const { contents } = config.modResults;
const installIndex = contents.indexOf(installScript);
if (
installIndex === -1 &&
contents.indexOf(
'config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"'
) === -1
) {
// 如果没有 post_install 且没有 arm64 忽略脚本,则插入
config.modResults.contents += `
${installScript}
${postInstallScript}
`;
} else if (
installIndex !== -1 &&
contents.indexOf(
'config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"'
) === -1
) {
// 如果有 post_install 但没有 arm64 忽略脚本,则在 post_install 后插入
config.modResults.contents =
contents.slice(0, installIndex + installScript.length) +
postInstallScript +
contents.slice(installIndex + installScript.length);
} else {
console.log('[MX_JPush_Expo] post_install 脚本已经存在,跳过添加.');
}
return config;
});
const setInterface = config => {
return withAppDelegate(config, config => {
const implementationIndex = config.modResults.contents.indexOf(
'@implementation AppDelegate'
);
if (
implementationIndex !== -1 &&
config.modResults.contents.indexOf(
'@interface AppDelegate ()<JPUSHRegisterDelegate>'
) === -1
) {
console.log('\n[MX_JPush_Expo] 配置 AppDelegate interface ... ');
const injectionCode = `
@interface AppDelegate () <JPUSHRegisterDelegate>
@end
`;
// 在 @implementation AppDelegate 前插入代码
const updatedData =
config.modResults.contents.slice(0, implementationIndex) +
injectionCode +
config.modResults.contents.slice(implementationIndex);
config.modResults.contents = updatedData;
}
if (implementationIndex === -1) {
console.error('未找到 @implementation AppDelegate');
}
return config;
});
};
// 配置 iOS AppDelegate
const setAppDelegate = config =>
withAppDelegate(config, config => {
if (
config.modResults.contents.indexOf(
'#import <UserNotifications/UserNotifications.h>'
) === -1
) {
console.log('\n[MX_JPush_Expo] 配置 AppDelegate import ... ');
config.modResults.contents =
`#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>
#import <RCTJPushModule.h>
#import <React/RCTBridge.h>
#import <React/RCTRootView.h>
` + config.modResults.contents;
}
if (
config.modResults.contents.indexOf(
'JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];'
) === -1
) {
console.log(
'\n[MX_JPush_Expo] 配置 AppDelegate didFinishLaunchingWithOptions ... '
);
const didFinishLaunchingWithOptionsResult =
config.modResults.contents.match(
/didFinishLaunchingWithOptions([\s\S]*)launchOptions\n\{\n/
);
const [didFinishLaunchingWithOptions] =
didFinishLaunchingWithOptionsResult;
const didFinishLaunchingWithOptionsIndex =
didFinishLaunchingWithOptionsResult.index;
const didFinishLaunchingWithOptionsStartIndex =
didFinishLaunchingWithOptionsIndex +
didFinishLaunchingWithOptions.length;
config.modResults.contents =
config.modResults.contents.slice(
0,
didFinishLaunchingWithOptionsStartIndex
) +
` // JPush初始化配置
[JPUSHService setupWithOption:launchOptions appKey:@"${JPUSH_APPKEY}" channel:@"${JPUSH_CHANNEL}" apsForProduction:YES];
// APNS 注册实体配置
JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
if (@available(iOS 12.0, *)) {
entity.types = JPAuthorizationOptionAlert | JPAuthorizationOptionBadge | JPAuthorizationOptionSound;
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
// 监听远程通知和响应通知
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
` +
config.modResults.contents.slice(
didFinishLaunchingWithOptionsStartIndex
);
} else {
console.log('\n[MX_JPush_Expo] 配置 AppDelegate appKey & channel ... ');
config.modResults.contents = config.modResults.contents.replace(
/appKey\:\@\"(.*)\" channel\:\@\"(.*)\" /,
`appKey:@"${JPUSH_APPKEY}" channel:@"${JPUSH_CHANNEL}" `
);
}
if (
config.modResults.contents.indexOf(
'return [super application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];'
) > -1
) {
config.modResults.contents = config.modResults.contents.replace(
'return [super application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];',
'[JPUSHService registerDeviceToken:deviceToken];'
);
}
if (
config.modResults.contents.indexOf(
'return [super application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];'
) > -1
) {
config.modResults.contents = config.modResults.contents.replace(
'return [super application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];',
`
// iOS 10 以下 Required
NSLog(@"iOS 7 APNS");
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
`
);
}
if (config.modResults.contents.indexOf('JPush start') === -1) {
console.log('\n[MX_JPush_Expo] 配置 AppDelegate other ... ');
config.modResults.contents = config.modResults.contents.replace(
/\@end([\n]*)$/,
`//************************************************JPush start************************************************
// iOS 10 及以上版本的通知处理
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// Apns
NSLog(@"iOS 10 APNS 前台收到消息");
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
}
else {
// 本地通知 todo
NSLog(@"iOS 10 本地通知 前台收到消息");
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert);
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// Apns
NSLog(@"iOS 10 APNS 消息事件回调");
[JPUSHService handleRemoteNotification:userInfo];
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
[[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
}
else {
// 本地通知
NSLog(@"iOS 10 本地通知 消息事件回调");
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
[[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
}
completionHandler();
}
// 自定义消息
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
}
//************************************************JPush end************************************************
@end
`
);
}
return config;
});
// 配置 Android AndroidManifest
const setAndroidManifest = config =>
withAndroidManifest(config, config => {
if (
AndroidConfig.Manifest.findMetaDataItem(
config.modResults.manifest.application[0],
'JPUSH_CHANNEL'
) === -1
) {
console.log('\n[MX_JPush_Expo] 配置 AndroidManifest JPUSH_CHANNEL ... ');
AndroidConfig.Manifest.addMetaDataItemToMainApplication(
config.modResults.manifest.application[0],
'JPUSH_CHANNEL',
'${JPUSH_CHANNEL}'
);
}
if (
AndroidConfig.Manifest.findMetaDataItem(
config.modResults.manifest.application[0],
'JPUSH_APPKEY'
) === -1
) {
console.log('\n[MX_JPush_Expo] 配置 AndroidManifest JPUSH_APPKEY ... ');
AndroidConfig.Manifest.addMetaDataItemToMainApplication(
config.modResults.manifest.application[0],
'JPUSH_APPKEY',
'${JPUSH_APPKEY}'
);
}
return config;
});
// 配置 Android build.gradle
const setAppBuildGradle = config =>
withAppBuildGradle(config, config => {
const defaultConfig = config.modResults.contents.match(
/defaultConfig([\s\S]*)versionName(.*)\n/
);
if (defaultConfig) {
const [startString] = defaultConfig;
const startStringLength = startString.length;
const startStringIndex =
config.modResults.contents.indexOf(startString) + startStringLength;
console.log('\n[MX_JPush_Expo] 配置 build.gradle appKey & channel ... ');
if (config.modResults.contents.indexOf('JPUSH_APPKEY') === -1) {
config.modResults.contents =
config.modResults.contents.slice(0, startStringIndex) +
` manifestPlaceholders = [
JPUSH_APPKEY: "${JPUSH_APPKEY}",
JPUSH_CHANNEL: "${JPUSH_CHANNEL}"
]\n` +
config.modResults.contents.slice(startStringIndex);
} else {
config.modResults.contents = config.modResults.contents.replace(
/manifestPlaceholders([\s\S]*)JPUSH_APPKEY([\s\S]*)JPUSH_CHANNEL(.*)"\n(.*)\]\n/,
`manifestPlaceholders = [
JPUSH_APPKEY: "${JPUSH_APPKEY}",
JPUSH_CHANNEL: "${JPUSH_CHANNEL}"
]\n`
);
}
} else
throw new Error(
'[MX_JPush_Expo] 无法完成 build.gradle - defaultConfig 配置'
);
const dependencies = config.modResults.contents.match(/dependencies {\n/);
if (dependencies) {
const [startString] = dependencies;
const startStringLength = startString.length;
const startStringIndex =
config.modResults.contents.indexOf(startString) + startStringLength;
if (
config.modResults.contents.indexOf(
`implementation project(':jpush-react-native')`
) === -1
) {
console.log(
'\n[MX_JPush_Expo] 配置 build.gradle dependencies jpush-react-native ... '
);
config.modResults.contents =
config.modResults.contents.slice(0, startStringIndex) +
` implementation project(':jpush-react-native')\n` +
config.modResults.contents.slice(startStringIndex);
}
if (
config.modResults.contents.indexOf(
`implementation project(':jcore-react-native')`
) === -1
) {
console.log(
'\n[MX_JPush_Expo] 配置 build.gradle dependencies jcore-react-native ... '
);
config.modResults.contents =
config.modResults.contents.slice(0, startStringIndex) +
` implementation project(':jcore-react-native')\n` +
config.modResults.contents.slice(startStringIndex);
}
} else
throw new Error(
'[MX_JPush_Expo] 无法完成 build.gradle dependencies 配置'
);
return config;
});
// 配置 Android settings.gradle
const setSettingsGradle = config =>
withSettingsGradle(config, config => {
if (
config.modResults.contents.indexOf(`include ':jpush-react-native'`) === -1
) {
console.log(
'\n[MX_JPush_Expo] 配置 settings.gradle include jpush-react-native ... '
);
config.modResults.contents =
config.modResults.contents +
`
include ':jpush-react-native'
project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')`;
}
if (
config.modResults.contents.indexOf(`include ':jcore-react-native'`) === -1
) {
console.log(
'\n[MX_JPush_Expo] 配置 settings.gradle include jcore-react-native ... '
);
config.modResults.contents =
config.modResults.contents +
`
include ':jcore-react-native'
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')`;
}
return config;
});
module.exports = withJPush;