-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpatch-capacitor-local-notifications.ts
More file actions
254 lines (246 loc) · 9.5 KB
/
patch-capacitor-local-notifications.ts
File metadata and controls
254 lines (246 loc) · 9.5 KB
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
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
const ROOT_DIR = resolve(process.cwd());
const TARGET_FILE = resolve(
ROOT_DIR,
"node_modules/@capacitor/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsPlugin.swift",
);
const HANDLER_FILE = resolve(
ROOT_DIR,
"node_modules/@capacitor/local-notifications/ios/Sources/LocalNotificationsPlugin/LocalNotificationsHandler.swift",
);
const REPLACEMENTS: ReadonlyArray<[string, string]> = [
[
[
' guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }) else {\n',
' call.reject("Must provide notifications array as notifications option")\n',
" return\n",
" }\n",
].join(""),
[
' guard call.getValue("notifications") != nil else {\n',
' call.error("Must provide notifications array as notifications option")\n',
" return\n",
" }\n",
' let notifications = call.getArray("notifications", []).compactMap({ $0 as? JSObject })\n',
].join(""),
],
[
[
' guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }), notifications.count > 0 else {\n',
' call.reject("Must supply notifications to cancel")\n',
" return\n",
" }\n",
].join(""),
[
' guard call.getValue("notifications") != nil else {\n',
' call.error("Must supply notifications to cancel")\n',
" return\n",
" }\n",
' let notifications = call.getArray("notifications", []).compactMap({ $0 as? JSObject })\n',
" guard notifications.count > 0 else {\n",
' call.error("Must supply notifications to cancel")\n',
" return\n",
" }\n",
].join(""),
],
[
[
' guard let notifications = call.getArray("notifications")?.compactMap({ $0 as? JSObject }), notifications.count > 0 else {\n',
' call.reject("Must supply notifications to remove")\n',
" return\n",
" }\n",
].join(""),
[
' guard call.getValue("notifications") != nil else {\n',
' call.error("Must supply notifications to remove")\n',
" return\n",
" }\n",
' let notifications = call.getArray("notifications", []).compactMap({ $0 as? JSObject })\n',
" guard notifications.count > 0 else {\n",
' call.error("Must supply notifications to remove")\n',
" return\n",
" }\n",
].join(""),
],
[
[
' guard let types = call.getArray("types")?.compactMap({ $0 as? JSObject }) else {\n',
" return\n",
" }\n",
].join(""),
[
' guard call.getValue("types") != nil else {\n',
" return\n",
" }\n",
' let types = call.getArray("types", []).compactMap({ $0 as? JSObject })\n',
].join(""),
],
[
"return bridge?.localURL(fromWebURL: webURL)",
[
" switch webURL.scheme {",
' case "res":',
" return bridge?.config.appLocation.appendingPathComponent(webURL.path)",
' case "file":',
" return webURL",
" default:",
" return nil",
" }",
].join("\n"),
],
];
const HANDLER_PATCH = {
search:
` func makePendingNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {\n` +
` var notification: JSObject = [\n` +
` "id": Int(request.identifier) ?? -1,\n` +
` "title": request.content.title,\n` +
` "body": request.content.body\n` +
` ]\n` +
`\n` +
` if let userInfo = JSTypes.coerceDictionaryToJSObject(request.content.userInfo) {\n` +
` var extra = userInfo["cap_extra"] as? JSObject ?? userInfo\n` +
`\n` +
` // check for any dates and convert them to strings\n` +
` for(key, value) in extra {\n` +
` if let date = value as? Date {\n` +
` let dateString = ISO8601DateFormatter().string(from: date)\n` +
` extra[key] = dateString\n` +
` }\n` +
` }\n` +
`\n` +
` notification["extra"] = extra\n` +
`\n` +
` if var schedule = userInfo["cap_schedule"] as? JSObject {\n` +
` // convert schedule at date to string\n` +
` if let date = schedule["at"] as? Date {\n` +
` let dateString = ISO8601DateFormatter().string(from: date)\n` +
` schedule["at"] = dateString\n` +
` }\n` +
`\n` +
` notification["schedule"] = schedule\n` +
` }\n` +
` }\n` +
`\n` +
` return notification\n` +
`\n` +
` }\n`,
replace:
` func makePendingNotificationRequestJSObject(_ request: UNNotificationRequest) -> JSObject {\n` +
` var notification: JSObject = [\n` +
` "id": Int(request.identifier) ?? -1,\n` +
` "title": request.content.title,\n` +
` "body": request.content.body\n` +
` ]\n` +
`\n` +
` if let userInfo = makeJSObject(from: request.content.userInfo) {\n` +
` var extra = userInfo["cap_extra"] as? JSObject ?? userInfo\n` +
`\n` +
` // check for any dates and convert them to strings\n` +
` for(key, value) in extra {\n` +
` if let date = value as? Date {\n` +
` let dateString = ISO8601DateFormatter().string(from: date)\n` +
` extra[key] = dateString\n` +
` }\n` +
` }\n` +
`\n` +
` notification["extra"] = extra\n` +
`\n` +
` if var schedule = userInfo["cap_schedule"] as? JSObject {\n` +
` // convert schedule at date to string\n` +
` if let date = schedule["at"] as? Date {\n` +
` let dateString = ISO8601DateFormatter().string(from: date)\n` +
` schedule["at"] = dateString\n` +
` }\n` +
`\n` +
` notification["schedule"] = schedule\n` +
` }\n` +
` }\n` +
`\n` +
` return notification\n` +
`\n` +
` }\n` +
`\n` +
` private func makeJSObject(from dictionary: [AnyHashable: Any]?) -> JSObject? {\n` +
` guard let dictionary else {\n` +
` return nil\n` +
` }\n` +
`\n` +
` var result = JSObject()\n` +
` for (key, value) in dictionary {\n` +
` guard let stringKey = key as? String else {\n` +
` continue\n` +
` }\n` +
`\n` +
` if let jsValue = makeJSValue(from: value) {\n` +
` result[stringKey] = jsValue\n` +
` }\n` +
` }\n` +
`\n` +
` return result\n` +
` }\n` +
`\n` +
` private func makeJSValue(from value: Any) -> JSValue? {\n` +
` switch value {\n` +
` case let string as String:\n` +
` return string\n` +
` case let bool as Bool:\n` +
` return bool\n` +
` case let int as Int:\n` +
` return int\n` +
` case let float as Float:\n` +
` return float\n` +
` case let double as Double:\n` +
` return double\n` +
` case let number as NSNumber:\n` +
` return number\n` +
` case let date as Date:\n` +
` return date\n` +
` case is NSNull:\n` +
` return NSNull()\n` +
` case let array as [Any]:\n` +
` return makeJSArray(from: array)\n` +
` case let dictionary as [AnyHashable: Any]:\n` +
` return makeJSObject(from: dictionary)\n` +
` default:\n` +
` return nil\n` +
` }\n` +
` }\n` +
`\n` +
` private func makeJSArray(from array: [Any]) -> JSArray {\n` +
` var result = JSArray()\n` +
` for value in array {\n` +
` if let jsValue = makeJSValue(from: value) {\n` +
` result.append(jsValue)\n` +
` }\n` +
` }\n` +
`\n` +
` return result\n` +
` }\n`,
};
if (!existsSync(TARGET_FILE)) {
console.log(`Skipping Capacitor local-notifications patch; missing file: ${TARGET_FILE}`);
process.exit(0);
}
for (const targetFile of [TARGET_FILE, HANDLER_FILE]) {
if (!existsSync(targetFile)) {
console.log(`Skipping Capacitor local-notifications patch; missing file: ${targetFile}`);
continue;
}
const original = readFileSync(targetFile, "utf8");
let updated = original;
if (targetFile === TARGET_FILE) {
for (const [pattern, replacement] of REPLACEMENTS) {
updated = updated.replace(pattern, replacement);
}
} else if (!updated.includes("private func makeJSObject(from")) {
updated = updated.replace(HANDLER_PATCH.search, HANDLER_PATCH.replace);
}
if (updated !== original) {
writeFileSync(targetFile, updated);
console.log(`Patched ${targetFile}`);
} else {
console.log(`No patch needed for ${targetFile}`);
}
}