cordova-plugin-pushizo 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +495 -350
- package/package.json +54 -54
- package/plugin.xml +96 -96
- package/src/android/com/pushizo/cordova/PushizoBridgeCore.kt +856 -493
- package/src/android/com/pushizo/cordova/PushizoConfigStore.kt +41 -41
- package/src/android/com/pushizo/cordova/PushizoCordovaPlugin.kt +163 -141
- package/src/android/com/pushizo/cordova/PushizoInitProvider.kt +34 -34
- package/src/android/pushizo.gradle +6 -6
- package/src/ios/PushizoAppDelegateHook.swift +54 -54
- package/src/ios/PushizoBridgeCore.swift +719 -465
- package/src/ios/PushizoCordovaPlugin.swift +186 -157
- package/types/index.d.ts +76 -54
- package/www/core.d.ts +169 -11
- package/www/core.js +413 -10
- package/www/definitions.d.ts +240 -0
|
@@ -1,465 +1,719 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import Pushizo
|
|
3
|
-
// SPM: ayrı modül; CocoaPods alt spec'leri tek "Pushizo" modülünde derlenir.
|
|
4
|
-
#if canImport(PushizoCore)
|
|
5
|
-
import PushizoCore
|
|
6
|
-
#endif
|
|
7
|
-
import UIKit
|
|
8
|
-
import UserNotifications
|
|
9
|
-
|
|
10
|
-
/// Köprü metot sonucu: başarıda isteğe bağlı sözlük, hatada kod ve mesaj.
|
|
11
|
-
enum PushizoBridgeResult {
|
|
12
|
-
case success([String: Any]?)
|
|
13
|
-
case failure(code: String, message: String)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/// JS `initialize` ayarlarının kalıcı kopyası (yalnızca herkese açık değerler; gizli bilgi tutulmaz).
|
|
17
|
-
struct PushizoBridgeConfig {
|
|
18
|
-
static let defaultsKey = "pushizo_bridge_config"
|
|
19
|
-
let appId: String
|
|
20
|
-
let apiUrl: String?
|
|
21
|
-
let requiresPrivacyConsent: Bool
|
|
22
|
-
let logLevel: String
|
|
23
|
-
|
|
24
|
-
func save() {
|
|
25
|
-
var dict: [String: Any] = ["appId": appId, "requiresPrivacyConsent": requiresPrivacyConsent, "logLevel": logLevel]
|
|
26
|
-
if let apiUrl { dict["apiUrl"] = apiUrl }
|
|
27
|
-
UserDefaults.standard.set(dict, forKey: Self.defaultsKey)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static func load() -> PushizoBridgeConfig? {
|
|
31
|
-
guard let dict = UserDefaults.standard.dictionary(forKey: defaultsKey),
|
|
32
|
-
let appId = dict["appId"] as? String, !appId.isEmpty else { return nil }
|
|
33
|
-
return PushizoBridgeConfig(appId: appId, apiUrl: dict["apiUrl"] as? String,
|
|
34
|
-
requiresPrivacyConsent: dict["requiresPrivacyConsent"] as? Bool ?? false,
|
|
35
|
-
logLevel: dict["logLevel"] as? String ?? "warn")
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/// SDK'yı bu ayarlarla başlatır.
|
|
39
|
-
func initializeSdk() {
|
|
40
|
-
Pushizo.initialize(appId) { options in
|
|
41
|
-
if let apiUrl { options.apiUrl = apiUrl }
|
|
42
|
-
options.requiresPrivacyConsent = requiresPrivacyConsent
|
|
43
|
-
options.logLevel = PushizoBridgeCore.logLevel(logLevel)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
Köprüden bağımsız iOS çekirdeği: JS metot adlarını Pushizo iOS SDK çağrılarına çevirir ve SDK olaylarını
|
|
50
|
-
`emit(tür, veri)` ile köprüye iletir. SDK dinleyicileri kaldırılamadığından süreç başına bir kez eklenir ve
|
|
51
|
-
o anki etkin çekirdeğe yönlendirilir.
|
|
52
|
-
*/
|
|
53
|
-
final class PushizoBridgeCore: NSObject {
|
|
54
|
-
/// Olayların iletileceği etkin çekirdek (köprü yeniden yüklenirse güncellenir).
|
|
55
|
-
fileprivate static weak var active: PushizoBridgeCore?
|
|
56
|
-
private static var listenersRegistered = false
|
|
57
|
-
private static let maxPendingForeground = 32
|
|
58
|
-
private static let
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
private var
|
|
65
|
-
private var
|
|
66
|
-
private
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Pushizo.Notifications.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
Pushizo.
|
|
94
|
-
guard let core = Self.active else { return }
|
|
95
|
-
core.
|
|
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
|
-
event.
|
|
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
|
-
case "
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
let
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
case "
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
case "
|
|
247
|
-
|
|
248
|
-
case "
|
|
249
|
-
|
|
250
|
-
guard let
|
|
251
|
-
Pushizo.
|
|
252
|
-
case "
|
|
253
|
-
guard let
|
|
254
|
-
Pushizo.
|
|
255
|
-
|
|
256
|
-
case "
|
|
257
|
-
guard let
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
ok()
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
guard
|
|
267
|
-
|
|
268
|
-
ok()
|
|
269
|
-
case "
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
case "
|
|
273
|
-
|
|
274
|
-
case "
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
case "
|
|
284
|
-
|
|
285
|
-
inbox(reply) { Self.map(try await Pushizo.
|
|
286
|
-
case "
|
|
287
|
-
inbox(reply) { ["
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
case "
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
case "
|
|
299
|
-
case "
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
case "
|
|
312
|
-
|
|
313
|
-
case "
|
|
314
|
-
let
|
|
315
|
-
let
|
|
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
|
-
case "
|
|
346
|
-
case "
|
|
347
|
-
case "
|
|
348
|
-
case "
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
case "
|
|
358
|
-
guard let
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
reply(.
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
case "
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
case "
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
}
|
|
1
|
+
import Foundation
|
|
2
|
+
import Pushizo
|
|
3
|
+
// SPM: ayrı modül; CocoaPods alt spec'leri tek "Pushizo" modülünde derlenir.
|
|
4
|
+
#if canImport(PushizoCore)
|
|
5
|
+
import PushizoCore
|
|
6
|
+
#endif
|
|
7
|
+
import UIKit
|
|
8
|
+
import UserNotifications
|
|
9
|
+
|
|
10
|
+
/// Köprü metot sonucu: başarıda isteğe bağlı sözlük, hatada kod ve mesaj.
|
|
11
|
+
enum PushizoBridgeResult {
|
|
12
|
+
case success([String: Any]?)
|
|
13
|
+
case failure(code: String, message: String)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/// JS `initialize` ayarlarının kalıcı kopyası (yalnızca herkese açık değerler; gizli bilgi tutulmaz).
|
|
17
|
+
struct PushizoBridgeConfig {
|
|
18
|
+
static let defaultsKey = "pushizo_bridge_config"
|
|
19
|
+
let appId: String
|
|
20
|
+
let apiUrl: String?
|
|
21
|
+
let requiresPrivacyConsent: Bool
|
|
22
|
+
let logLevel: String
|
|
23
|
+
|
|
24
|
+
func save() {
|
|
25
|
+
var dict: [String: Any] = ["appId": appId, "requiresPrivacyConsent": requiresPrivacyConsent, "logLevel": logLevel]
|
|
26
|
+
if let apiUrl { dict["apiUrl"] = apiUrl }
|
|
27
|
+
UserDefaults.standard.set(dict, forKey: Self.defaultsKey)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static func load() -> PushizoBridgeConfig? {
|
|
31
|
+
guard let dict = UserDefaults.standard.dictionary(forKey: defaultsKey),
|
|
32
|
+
let appId = dict["appId"] as? String, !appId.isEmpty else { return nil }
|
|
33
|
+
return PushizoBridgeConfig(appId: appId, apiUrl: dict["apiUrl"] as? String,
|
|
34
|
+
requiresPrivacyConsent: dict["requiresPrivacyConsent"] as? Bool ?? false,
|
|
35
|
+
logLevel: dict["logLevel"] as? String ?? "warn")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// SDK'yı bu ayarlarla başlatır.
|
|
39
|
+
func initializeSdk() {
|
|
40
|
+
Pushizo.initialize(appId) { options in
|
|
41
|
+
if let apiUrl { options.apiUrl = apiUrl }
|
|
42
|
+
options.requiresPrivacyConsent = requiresPrivacyConsent
|
|
43
|
+
options.logLevel = PushizoBridgeCore.logLevel(logLevel)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
Köprüden bağımsız iOS çekirdeği: JS metot adlarını Pushizo iOS SDK çağrılarına çevirir ve SDK olaylarını
|
|
50
|
+
`emit(tür, veri)` ile köprüye iletir. SDK dinleyicileri kaldırılamadığından süreç başına bir kez eklenir ve
|
|
51
|
+
o anki etkin çekirdeğe yönlendirilir.
|
|
52
|
+
*/
|
|
53
|
+
final class PushizoBridgeCore: NSObject {
|
|
54
|
+
/// Olayların iletileceği etkin çekirdek (köprü yeniden yüklenirse güncellenir).
|
|
55
|
+
fileprivate static weak var active: PushizoBridgeCore?
|
|
56
|
+
private static var listenersRegistered = false
|
|
57
|
+
private static let maxPendingForeground = 32
|
|
58
|
+
private static let maxPlacementCache = 50
|
|
59
|
+
private static let notInitialized = "Önce Pushizo.initialize çağrılmalı"
|
|
60
|
+
|
|
61
|
+
private let emit: (String, [String: Any]) -> Void
|
|
62
|
+
/// JS tarafında ön plan dinleyicisi varsa gösterim JS kararına bırakılır.
|
|
63
|
+
var foregroundHandlerEnabled = false
|
|
64
|
+
private var foregroundSeq = 0
|
|
65
|
+
private var pendingForeground: [String: HSForegroundEvent] = [:]
|
|
66
|
+
private var pendingOrder: [String] = []
|
|
67
|
+
/// Son `placementsList` yanıtlarındaki mesajlar (kimliğe göre); raporlar bu nesnelerle yapılır, en eski kayıtlar düşürülür.
|
|
68
|
+
private var placementMessages: [String: HSInAppMessage] = [:]
|
|
69
|
+
private var placementOrder: [String] = []
|
|
70
|
+
private let lock = NSLock()
|
|
71
|
+
|
|
72
|
+
init(emit: @escaping (String, [String: Any]) -> Void) {
|
|
73
|
+
self.emit = emit
|
|
74
|
+
super.init()
|
|
75
|
+
Self.active = self
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// MARK: Olaylar
|
|
79
|
+
|
|
80
|
+
/// SDK başlatılmışsa (kayıtlı ayarlarla) dinleyicileri bağlar.
|
|
81
|
+
func attachIfInitialized() {
|
|
82
|
+
if Pushizo.isInitialized { registerListeners() }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private func registerListeners() {
|
|
86
|
+
Self.active = self
|
|
87
|
+
guard !Self.listenersRegistered else { return }
|
|
88
|
+
Self.listenersRegistered = true
|
|
89
|
+
Pushizo.Notifications.addClickListener { e in
|
|
90
|
+
Self.active?.emit("click", ["notification": Self.map(e.notification),
|
|
91
|
+
"result": ["actionId": Self.orNull(e.actionId), "url": Self.orNull(e.url)]])
|
|
92
|
+
}
|
|
93
|
+
Pushizo.Notifications.addForegroundLifecycleListener { e in
|
|
94
|
+
guard let core = Self.active else { return }
|
|
95
|
+
core.onForeground(e)
|
|
96
|
+
}
|
|
97
|
+
Pushizo.User.pushSubscription.addObserver { s in
|
|
98
|
+
guard let core = Self.active else { return }
|
|
99
|
+
core.emit("pushSubscriptionChange", Self.map(s))
|
|
100
|
+
}
|
|
101
|
+
// Yerel SDK izin gözlemcisi: izin isteği sonucu ve uygulama yeniden etkinleştiğinde (izin Ayarlar'dan
|
|
102
|
+
// değişmiş olabilir) etkin izin değiştiyse çağrılır.
|
|
103
|
+
Pushizo.Notifications.addPermissionObserver { granted in
|
|
104
|
+
Self.active?.emit("permissionChange", ["permission": granted])
|
|
105
|
+
}
|
|
106
|
+
Pushizo.InAppMessages.addClickListener { e in
|
|
107
|
+
Self.active?.emit("inAppClick", ["message": ["id": e.messageId], "result": ["actionId": e.actionId, "url": Self.orNull(e.url)]])
|
|
108
|
+
}
|
|
109
|
+
Pushizo.InAppMessages.addLifecycleListener(PushizoBridgeLifecycleForwarder.shared)
|
|
110
|
+
// Canlı sohbet olayları → JS `chatEvent`; sohbet bildirimi tıklaması → `chatOpen` (cephe bekletir).
|
|
111
|
+
Pushizo.Chat.addListener { event in
|
|
112
|
+
var body = event.payload
|
|
113
|
+
body["type"] = event.type
|
|
114
|
+
Self.active?.emit("chatEvent", body)
|
|
115
|
+
}
|
|
116
|
+
Pushizo.Chat.addOpenListener { info in
|
|
117
|
+
Self.active?.emit("chatOpen", info.dictionary)
|
|
118
|
+
}
|
|
119
|
+
// Özellik bayrakları yenilendi → JS `featureFlagsChange`; bildirim kutusu değişti → `inboxChange`
|
|
120
|
+
// (`{ type, itemId?, unreadCount }`). Yerel SDK ikisini de ana iş parçacığında çağırır.
|
|
121
|
+
Pushizo.FeatureFlags.addObserver {
|
|
122
|
+
Self.active?.emit("featureFlagsChange", [:])
|
|
123
|
+
}
|
|
124
|
+
Pushizo.Inbox.addObserver { event in
|
|
125
|
+
Self.active?.emit("inboxChange", event.dictionary)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private func onForeground(_ event: HSForegroundEvent) {
|
|
130
|
+
lock.lock()
|
|
131
|
+
foregroundSeq += 1
|
|
132
|
+
let id = "fg-\(foregroundSeq)"
|
|
133
|
+
let pending = foregroundHandlerEnabled
|
|
134
|
+
if pending {
|
|
135
|
+
// Yerel gösterim ertelenir; JS `display()` çağırırsa veya hiçbir dinleyici engellemezse gösterilir
|
|
136
|
+
// (yanıt gelmezse SDK 25 sn sonra bildirimi göstermeden kapatır).
|
|
137
|
+
event.preventDefault()
|
|
138
|
+
pendingForeground[id] = event
|
|
139
|
+
pendingOrder.append(id)
|
|
140
|
+
while pendingOrder.count > Self.maxPendingForeground {
|
|
141
|
+
pendingForeground.removeValue(forKey: pendingOrder.removeFirst())
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
lock.unlock()
|
|
145
|
+
emit("foregroundWillDisplay", ["notificationId": id, "pending": pending, "notification": Self.map(event.notification)])
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private func takePending(_ id: String) -> HSForegroundEvent? {
|
|
149
|
+
lock.lock()
|
|
150
|
+
defer { lock.unlock() }
|
|
151
|
+
pendingOrder.removeAll { $0 == id }
|
|
152
|
+
return pendingForeground.removeValue(forKey: id)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private func rememberPlacements(_ messages: [HSInAppMessage]) {
|
|
156
|
+
lock.lock()
|
|
157
|
+
defer { lock.unlock() }
|
|
158
|
+
for message in messages {
|
|
159
|
+
placementOrder.removeAll { $0 == message.id }
|
|
160
|
+
placementMessages[message.id] = message
|
|
161
|
+
placementOrder.append(message.id)
|
|
162
|
+
}
|
|
163
|
+
while placementOrder.count > Self.maxPlacementCache {
|
|
164
|
+
placementMessages.removeValue(forKey: placementOrder.removeFirst())
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// Rapor için mesajı son `placementsList` yanıtlarından bulur; bulunamazsa yok sayılır (yalnızca debug log).
|
|
169
|
+
private func placementMessage(_ id: String) -> HSInAppMessage? {
|
|
170
|
+
lock.lock()
|
|
171
|
+
defer { lock.unlock() }
|
|
172
|
+
guard let message = placementMessages[id] else {
|
|
173
|
+
if HSLog.level.rawValue >= HSLog.Level.debug.rawValue { print("[Pushizo] yerleşim mesajı bulunamadı: \(id)") }
|
|
174
|
+
return nil
|
|
175
|
+
}
|
|
176
|
+
return message
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
fileprivate func emitLifecycle(_ event: String, _ message: HSInAppMessage) {
|
|
180
|
+
emit("inAppLifecycle", ["event": event, "message": ["id": message.id]])
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// MARK: Metotlar
|
|
184
|
+
|
|
185
|
+
/// Metodu çalıştırır; bilinmeyen metotta false döner.
|
|
186
|
+
func handle(_ method: String, _ args: [String: Any], reply: @escaping (PushizoBridgeResult) -> Void) -> Bool {
|
|
187
|
+
func ok(_ data: [String: Any]? = nil) { reply(.success(data)) }
|
|
188
|
+
func invalid(_ message: String) { reply(.failure(code: "INVALID_ARGUMENT", message: message)) }
|
|
189
|
+
func string(_ key: String) -> String? {
|
|
190
|
+
guard let value = args[key] as? String, !value.trimmingCharacters(in: .whitespaces).isEmpty else { return nil }
|
|
191
|
+
return value
|
|
192
|
+
}
|
|
193
|
+
func stringMap(_ key: String) -> [String: String] {
|
|
194
|
+
(args[key] as? [String: Any] ?? [:]).compactMapValues { value in
|
|
195
|
+
if value is NSNull { return nil }
|
|
196
|
+
return (value as? String) ?? "\(value)"
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
func stringList(_ key: String) -> [String] { (args[key] as? [Any] ?? []).compactMap { $0 as? String } }
|
|
200
|
+
|
|
201
|
+
switch method {
|
|
202
|
+
case "initialize":
|
|
203
|
+
guard let appId = string("appId") else { invalid("appId gerekli"); return true }
|
|
204
|
+
let config = PushizoBridgeConfig(appId: appId, apiUrl: string("apiUrl"),
|
|
205
|
+
requiresPrivacyConsent: args["requiresPrivacyConsent"] as? Bool ?? false,
|
|
206
|
+
logLevel: string("logLevel") ?? "warn")
|
|
207
|
+
// Soğuk başlatmada (bildirim tıklaması) SDK köprü yüklenir yüklenmez başlatılabilsin diye saklanır.
|
|
208
|
+
config.save()
|
|
209
|
+
config.initializeSdk()
|
|
210
|
+
registerListeners()
|
|
211
|
+
ok()
|
|
212
|
+
case "isInitialized": ok(["initialized": Pushizo.isInitialized])
|
|
213
|
+
case "setConsentGiven": Pushizo.setConsentGiven(args["given"] as? Bool ?? false); ok()
|
|
214
|
+
case "login":
|
|
215
|
+
guard let externalId = string("externalId") else { invalid("externalId gerekli"); return true }
|
|
216
|
+
let identityHash = string("identityHash"), jwt = string("jwt")
|
|
217
|
+
// Kanıt app kipine uymuyorsa Android köprüsündeki gibi reddedilir (yerel SDK yalnızca loglar).
|
|
218
|
+
if let message = Pushizo.loginProofError(identityHash: identityHash, jwt: jwt) {
|
|
219
|
+
reply(.failure(code: "NOT_READY", message: message)); return true
|
|
220
|
+
}
|
|
221
|
+
guard let profile = args["profile"] as? [String: Any] else {
|
|
222
|
+
Pushizo.login(externalId, identityHash: identityHash, jwt: jwt); ok(); return true
|
|
223
|
+
}
|
|
224
|
+
// Profil (isteğe bağlı): yerel `login(_:profile:identityHash:jwt:)` e-posta, telefon ve takma adları girişten önce
|
|
225
|
+
// doğrular (yarım kalmış kimlik değişimi olmaz), profil işlemlerini girişten sonra kuyruğa yazar.
|
|
226
|
+
let userProfile = HSUserProfile(email: Self.nonBlank(profile["email"]), sms: Self.nonBlank(profile["sms"]),
|
|
227
|
+
tags: Self.stringMap(profile["tags"]), aliases: Self.stringMap(profile["aliases"]))
|
|
228
|
+
do {
|
|
229
|
+
try Pushizo.login(externalId, profile: userProfile, identityHash: identityHash, jwt: jwt)
|
|
230
|
+
ok()
|
|
231
|
+
} catch HSError.invalidArgument(let message) {
|
|
232
|
+
invalid(message)
|
|
233
|
+
} catch {
|
|
234
|
+
reply(.failure(code: "NOT_READY", message: Self.notInitialized))
|
|
235
|
+
}
|
|
236
|
+
case "updateUserJwt":
|
|
237
|
+
guard let jwt = string("jwt") else { invalid("jwt gerekli"); return true }
|
|
238
|
+
do {
|
|
239
|
+
try Pushizo.updateUserJwt(jwt); ok()
|
|
240
|
+
} catch {
|
|
241
|
+
reply(.failure(code: "NOT_READY", message: "Önce login ile kullanıcı kimliklendirilmeli"))
|
|
242
|
+
}
|
|
243
|
+
case "logout": Pushizo.logout(); ok()
|
|
244
|
+
|
|
245
|
+
case "addTags": Pushizo.User.addTags(stringMap("tags")); ok()
|
|
246
|
+
case "removeTags": Pushizo.User.removeTags(stringList("keys")); ok()
|
|
247
|
+
case "getTags": ok(["tags": Pushizo.User.getTags()])
|
|
248
|
+
case "addAliases": Pushizo.User.addAliases(stringMap("aliases")); ok()
|
|
249
|
+
case "removeAlias":
|
|
250
|
+
guard let label = string("label") else { invalid("label gerekli"); return true }
|
|
251
|
+
Pushizo.User.removeAlias(label); ok()
|
|
252
|
+
case "setLanguage":
|
|
253
|
+
guard let code = string("code") else { invalid("code gerekli"); return true }
|
|
254
|
+
Pushizo.User.setLanguage(code); ok()
|
|
255
|
+
// Kullanıcı özelliği: `null` (NSNull) özelliği siler; desteklenmeyen değer yerel SDK'da günlüğe yazılıp yok sayılır.
|
|
256
|
+
case "setUserProperty":
|
|
257
|
+
guard let key = string("key") else { invalid("key gerekli"); return true }
|
|
258
|
+
Pushizo.User.setProperty(key, args["value"]); ok()
|
|
259
|
+
case "addEmail", "removeEmail":
|
|
260
|
+
let email = args["email"] as? String ?? ""
|
|
261
|
+
guard HSContacts.normalizeEmail(email) != nil else { invalid("Geçersiz e-posta adresi."); return true }
|
|
262
|
+
if method == "addEmail" { Pushizo.User.addEmail(email) } else { Pushizo.User.removeEmail(email) }
|
|
263
|
+
ok()
|
|
264
|
+
case "addSms", "removeSms":
|
|
265
|
+
let phone = args["phoneNumber"] as? String ?? ""
|
|
266
|
+
guard HSContacts.normalizePhone(phone) != nil else { invalid("Geçersiz telefon numarası (E.164 bekleniyor)."); return true }
|
|
267
|
+
if method == "addSms" { Pushizo.User.addSms(phone) } else { Pushizo.User.removeSms(phone) }
|
|
268
|
+
ok()
|
|
269
|
+
case "getUser": ok(["id": Self.orNull(Pushizo.User.id), "externalId": Self.orNull(Pushizo.User.externalId)])
|
|
270
|
+
case "getContacts": ok(["emails": Pushizo.User.emails, "smsNumbers": Pushizo.User.smsNumbers])
|
|
271
|
+
// Tercihler (ADR-0019 §4): sahiplik kanıtı yerel SDK'da eklenir; doğrulama hatası INVALID_ARGUMENT, abonelik yoksa NOT_READY.
|
|
272
|
+
case "getPreferences":
|
|
273
|
+
inbox(reply) { Self.map(try await Pushizo.User.getPreferences()) }
|
|
274
|
+
case "setPreferences":
|
|
275
|
+
guard let raw = args["preferences"] as? [Any] else { invalid("preferences gerekli"); return true }
|
|
276
|
+
let items = raw.compactMap { $0 as? [String: Any] }
|
|
277
|
+
guard items.count == raw.count else { invalid("preferences öğeleri nesne olmalı"); return true }
|
|
278
|
+
let inputs = items.map {
|
|
279
|
+
HSPreferenceInput(category: $0["category"] as? String ?? "", channel: $0["channel"] as? String, optedIn: $0["optedIn"] as? Bool ?? false)
|
|
280
|
+
}
|
|
281
|
+
inbox(reply) { Self.map(try await Pushizo.User.setPreferences(inputs)) }
|
|
282
|
+
// Reklam kişiselleştirme rızası (ADR-0037): tercih ucundaki ayrılmış `ads_personalization` anahtarı.
|
|
283
|
+
case "setAdsPersonalizationConsent":
|
|
284
|
+
let granted = args["granted"] as? Bool ?? false
|
|
285
|
+
inbox(reply) { Self.map(try await Pushizo.User.setAdsPersonalizationConsent(granted)) }
|
|
286
|
+
case "getConsents":
|
|
287
|
+
inbox(reply) { ["consents": try await Pushizo.User.getConsents().map { Self.map($0) }] }
|
|
288
|
+
// Fire OS ADM yalnızca Android'dedir: iOS'ta etkisiz, hata yok.
|
|
289
|
+
case "registerAdm": ok()
|
|
290
|
+
// Reklam kimliği (IDFA): nil veya sıfır kimlik sunucudaki kaydı siler.
|
|
291
|
+
case "setAdvertisingId": Pushizo.Ads.setAdvertisingId(string("id")); ok()
|
|
292
|
+
|
|
293
|
+
case "getPushSubscription":
|
|
294
|
+
Task {
|
|
295
|
+
let state = await Pushizo.User.pushSubscription.state()
|
|
296
|
+
DispatchQueue.main.async { ok(Self.map(state)) }
|
|
297
|
+
}
|
|
298
|
+
case "optIn": Pushizo.User.pushSubscription.optIn(); ok()
|
|
299
|
+
case "optOut": Pushizo.User.pushSubscription.optOut(); ok()
|
|
300
|
+
|
|
301
|
+
case "getPermission":
|
|
302
|
+
Task {
|
|
303
|
+
let status = await Pushizo.Notifications.permission()
|
|
304
|
+
DispatchQueue.main.async { ok(["permission": Self.granted(status)]) }
|
|
305
|
+
}
|
|
306
|
+
case "requestPermission":
|
|
307
|
+
// İzin değiştiyse `permissionChange` olayını yerel SDK izin gözlemcisi gönderir.
|
|
308
|
+
Pushizo.Notifications.requestPermission { granted in
|
|
309
|
+
DispatchQueue.main.async { ok(["permission": granted]) }
|
|
310
|
+
}
|
|
311
|
+
case "clearAll": Pushizo.Notifications.clearAll(); ok()
|
|
312
|
+
// iOS kimliği `UNNotificationRequest.identifier` metnidir; tamsayı verilirse metne çevrilerek aranır.
|
|
313
|
+
case "removeNotification":
|
|
314
|
+
let requested = string("identifier") ?? (args["id"] as? NSNumber).map { "\($0.intValue)" }
|
|
315
|
+
guard let identifier = requested else { invalid("identifier gerekli"); return true }
|
|
316
|
+
Pushizo.Notifications.removeNotification(identifier: identifier); ok()
|
|
317
|
+
case "removeGroupedNotifications":
|
|
318
|
+
guard let group = string("group") else { invalid("group gerekli"); return true }
|
|
319
|
+
Pushizo.Notifications.removeGroupedNotifications(group); ok()
|
|
320
|
+
// Uygulama rozeti; bildirim uzantısıyla paylaşılan sayaç da eşitlenir (0 rozeti sıfırlar).
|
|
321
|
+
case "setBadgeCount":
|
|
322
|
+
guard let count = (args["count"] as? NSNumber)?.intValue, count >= 0 else { invalid("count negatif olmayan tam sayı olmalı"); return true }
|
|
323
|
+
Pushizo.Notifications.setBadgeCount(count); ok(["success": true])
|
|
324
|
+
// Uygulama simgesindeki güncel rozet (UIKit; ana iş parçacığında okunur).
|
|
325
|
+
case "getBadgeCount":
|
|
326
|
+
DispatchQueue.main.async { ok(["count": UIApplication.shared.applicationIconBadgeNumber]) }
|
|
327
|
+
case "setForegroundHandlerEnabled": foregroundHandlerEnabled = args["enabled"] as? Bool ?? false; ok()
|
|
328
|
+
case "displayNotification":
|
|
329
|
+
guard let id = string("notificationId") else { invalid("notificationId gerekli"); return true }
|
|
330
|
+
guard let event = takePending(id) else {
|
|
331
|
+
reply(.failure(code: "NOT_FOUND", message: "Bildirim bulunamadı veya zaten gösterildi."))
|
|
332
|
+
return true
|
|
333
|
+
}
|
|
334
|
+
event.display()
|
|
335
|
+
ok()
|
|
336
|
+
|
|
337
|
+
case "sendEvent":
|
|
338
|
+
guard let name = string("name") else { invalid("name gerekli"); return true }
|
|
339
|
+
// `eventId` (isteğe bağlı) tekilleştirme kimliğidir; biçim geçersizse yerel SDK kendi kimliğini üretir.
|
|
340
|
+
Pushizo.Session.sendEvent(name, properties: args["properties"] as? [String: Any] ?? [:],
|
|
341
|
+
value: (args["value"] as? NSNumber)?.doubleValue, eventId: string("eventId"))
|
|
342
|
+
ok()
|
|
343
|
+
case "setLogLevel": Pushizo.Debug.setLogLevel(Self.logLevel(args["level"] as? String)); ok()
|
|
344
|
+
|
|
345
|
+
case "addTriggers": Pushizo.InAppMessages.addTriggers(stringMap("triggers")); ok()
|
|
346
|
+
case "removeTriggers": Pushizo.InAppMessages.removeTriggers(stringList("keys")); ok()
|
|
347
|
+
case "clearTriggers": Pushizo.InAppMessages.clearTriggers(); ok()
|
|
348
|
+
case "getTriggers": ok(["triggers": Pushizo.InAppMessages.getTriggers()])
|
|
349
|
+
case "setScreen": Pushizo.InAppMessages.setScreen(args["name"] as? String); ok()
|
|
350
|
+
case "setInAppPaused": Pushizo.InAppMessages.paused = args["paused"] as? Bool ?? false; ok()
|
|
351
|
+
case "getInAppPaused": ok(["paused": Pushizo.InAppMessages.paused])
|
|
352
|
+
|
|
353
|
+
case "inboxList":
|
|
354
|
+
let limit = (args["limit"] as? NSNumber)?.intValue ?? 50
|
|
355
|
+
let cursor = string("cursor")
|
|
356
|
+
inbox(reply) { Self.map(try await Pushizo.Inbox.list(limit: limit, cursor: cursor)) }
|
|
357
|
+
case "inboxMarkRead":
|
|
358
|
+
guard let id = string("id") else { invalid("id gerekli"); return true }
|
|
359
|
+
inbox(reply) { Self.map(try await Pushizo.Inbox.markRead(id)) }
|
|
360
|
+
case "inboxMarkAllRead":
|
|
361
|
+
inbox(reply) { ["unreadCount": try await Pushizo.Inbox.markAllRead()] }
|
|
362
|
+
case "inboxArchive":
|
|
363
|
+
guard let id = string("id") else { invalid("id gerekli"); return true }
|
|
364
|
+
inbox(reply) { try await Pushizo.Inbox.archive(id); return nil }
|
|
365
|
+
case "inboxUnreadCount":
|
|
366
|
+
inbox(reply) { ["unreadCount": try await Pushizo.Inbox.unreadCount()] }
|
|
367
|
+
|
|
368
|
+
// İçerik kartları (ADR-0082).
|
|
369
|
+
case "contentCardsList":
|
|
370
|
+
let limit = (args["limit"] as? NSNumber)?.intValue ?? 50
|
|
371
|
+
inbox(reply) { Self.map(try await Pushizo.ContentCards.list(limit: limit)) }
|
|
372
|
+
case "contentCardEvent":
|
|
373
|
+
guard let id = string("id") else { invalid("id gerekli"); return true }
|
|
374
|
+
let event = string("event")
|
|
375
|
+
guard event == "impression" || event == "click" || event == "dismiss" else {
|
|
376
|
+
invalid("Kart olayı impression, click veya dismiss olmalı"); return true
|
|
377
|
+
}
|
|
378
|
+
inbox(reply) {
|
|
379
|
+
let card: HSContentCard
|
|
380
|
+
if event == "impression" { card = try await Pushizo.ContentCards.logImpression(id) }
|
|
381
|
+
else if event == "click" { card = try await Pushizo.ContentCards.logClick(id) }
|
|
382
|
+
else { card = try await Pushizo.ContentCards.dismiss(id) }
|
|
383
|
+
return Self.map(card)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Konum (v2 Faz 17b; yalnızca ön plan): yerel SDK başlatılmadan çağrılırsa sessizce yok sayılmaz, NOT_READY döner.
|
|
387
|
+
case "setLocationShared":
|
|
388
|
+
guard Pushizo.isInitialized else { reply(.failure(code: "NOT_READY", message: Self.notInitialized)); return true }
|
|
389
|
+
Pushizo.Location.setShared(args["shared"] as? Bool ?? false); ok()
|
|
390
|
+
case "isLocationShared": ok(["shared": Pushizo.Location.isShared])
|
|
391
|
+
case "requestLocationPermission":
|
|
392
|
+
guard Pushizo.isInitialized else { reply(.failure(code: "NOT_READY", message: Self.notInitialized)); return true }
|
|
393
|
+
// Açıklama uyarısı + `requestWhenInUseAuthorization`; sonuç ana iş parçacığında döner.
|
|
394
|
+
Pushizo.Location.requestPermission(rationale: string("rationale")) { granted in ok(["permission": granted]) }
|
|
395
|
+
// İzin durumu yerel SDK'dan okunur ("kullanırken" veya "her zaman" = ön plan, "her zaman" = arka plan); istem açmaz.
|
|
396
|
+
case "getLocationPermission":
|
|
397
|
+
ok(["permission": Pushizo.Location.permissionGranted, "background": Pushizo.Location.backgroundPermissionGranted])
|
|
398
|
+
case "requestBackgroundLocationPermission":
|
|
399
|
+
guard Pushizo.isInitialized else { reply(.failure(code: "NOT_READY", message: Self.notInitialized)); return true }
|
|
400
|
+
// "Her zaman" izni (Android arka plan izninin karşılığı; `rationale` iOS'ta kullanılmaz). Sonuç ana iş parçacığında döner.
|
|
401
|
+
Pushizo.Location.requestAlwaysPermission { granted in ok(["permission": granted]) }
|
|
402
|
+
// Arka plan bölge izleme (ADR-0065): varsayılan kapalı; Info.plist'e izin eklenmez.
|
|
403
|
+
case "setGeofencingEnabled":
|
|
404
|
+
guard Pushizo.isInitialized else { reply(.failure(code: "NOT_READY", message: Self.notInitialized)); return true }
|
|
405
|
+
Pushizo.Location.setGeofencingEnabled(args["enabled"] as? Bool ?? false); ok()
|
|
406
|
+
case "isGeofencingEnabled": ok(["enabled": Pushizo.Location.isGeofencingEnabled])
|
|
407
|
+
|
|
408
|
+
// Android teslimat güçlendirme (ADR-0092) iOS'ta yoktur: durum "desteklenmiyor", diğerleri etkisiz false.
|
|
409
|
+
case "deliveryStatus": ok(["supported": false])
|
|
410
|
+
case "deliveryShouldShowGuidance": ok(["show": false])
|
|
411
|
+
case "deliveryOpenSettings", "deliveryShowGuidance": ok(["opened": false])
|
|
412
|
+
|
|
413
|
+
// Özellik bayrakları (ADR-0066): değerler yerel SDK önbelleğinden okunur.
|
|
414
|
+
case "featureFlagsRefresh":
|
|
415
|
+
Task {
|
|
416
|
+
let success = await Pushizo.FeatureFlags.refresh()
|
|
417
|
+
DispatchQueue.main.async { ok(["success": success]) }
|
|
418
|
+
}
|
|
419
|
+
case "featureFlagsIsEnabled":
|
|
420
|
+
guard let key = string("key") else { invalid("key gerekli"); return true }
|
|
421
|
+
ok(["enabled": Pushizo.FeatureFlags.isEnabled(key, default: args["defaultValue"] as? Bool ?? false)])
|
|
422
|
+
case "featureFlagsGetValue":
|
|
423
|
+
guard let key = string("key") else { invalid("key gerekli"); return true }
|
|
424
|
+
ok(["value": Self.orNull(Pushizo.FeatureFlags.value(key))])
|
|
425
|
+
|
|
426
|
+
// Gömülü yerleşimler (ADR-0068): anahtar ve limit denetimi yerel SDK'dadır (HSError.invalidArgument → INVALID_ARGUMENT).
|
|
427
|
+
case "placementsList":
|
|
428
|
+
guard let key = string("key") else { invalid("key gerekli"); return true }
|
|
429
|
+
let limit = (args["limit"] as? NSNumber)?.intValue ?? 1
|
|
430
|
+
inbox(reply) {
|
|
431
|
+
let messages = try await Pushizo.Placements.list(key, limit: limit)
|
|
432
|
+
self.rememberPlacements(messages)
|
|
433
|
+
return ["messages": messages.map { Self.map($0) }]
|
|
434
|
+
}
|
|
435
|
+
case "placementsReportImpression":
|
|
436
|
+
guard let id = string("id") else { invalid("id gerekli"); return true }
|
|
437
|
+
if let message = placementMessage(id) { Pushizo.Placements.reportImpression(message) }
|
|
438
|
+
ok()
|
|
439
|
+
case "placementsReportClick":
|
|
440
|
+
guard let id = string("id"), let buttonId = string("buttonId") else { invalid("id ve buttonId gerekli"); return true }
|
|
441
|
+
if let message = placementMessage(id) { Pushizo.Placements.reportClick(message, buttonId: buttonId) }
|
|
442
|
+
ok()
|
|
443
|
+
case "placementsReportDismiss":
|
|
444
|
+
guard let id = string("id") else { invalid("id gerekli"); return true }
|
|
445
|
+
if let message = placementMessage(id) { Pushizo.Placements.reportDismiss(message) }
|
|
446
|
+
ok()
|
|
447
|
+
|
|
448
|
+
// Canlı sohbet (ADR-0040): kimlik, kuyruk, akış ve push yerel SDK'dadır.
|
|
449
|
+
case "chatStart":
|
|
450
|
+
let name = string("name"), email = string("email"), language = string("language")
|
|
451
|
+
inbox(reply) { try await Pushizo.Chat.start(name: name, email: email, language: language).dictionary }
|
|
452
|
+
case "chatGetConfig":
|
|
453
|
+
let language = string("language")
|
|
454
|
+
inbox(reply) { try await Pushizo.Chat.getConfig(language: language).dictionary }
|
|
455
|
+
case "chatGetConversation":
|
|
456
|
+
inbox(reply) { try await Pushizo.Chat.getConversation().dictionary }
|
|
457
|
+
case "chatGetMessages":
|
|
458
|
+
let after = (args["afterSeq"] as? NSNumber)?.intValue
|
|
459
|
+
let before = (args["beforeSeq"] as? NSNumber)?.intValue
|
|
460
|
+
let limit = (args["limit"] as? NSNumber)?.intValue ?? 50
|
|
461
|
+
inbox(reply) { try await Pushizo.Chat.getMessages(afterSeq: after, beforeSeq: before, limit: limit).dictionary }
|
|
462
|
+
case "chatSendMessage":
|
|
463
|
+
guard let text = string("text") else { invalid("text gerekli"); return true }
|
|
464
|
+
let optionId = string("optionId")
|
|
465
|
+
inbox(reply) { try await Pushizo.Chat.sendMessage(text, optionId: optionId).dictionary }
|
|
466
|
+
case "chatSendAttachment":
|
|
467
|
+
guard let fileName = string("fileName"), let mimeType = string("mimeType") else { invalid("fileName ve mimeType gerekli"); return true }
|
|
468
|
+
let text = string("text")
|
|
469
|
+
let data: Data
|
|
470
|
+
if let base64 = string("base64") {
|
|
471
|
+
guard let decoded = Data(base64Encoded: base64, options: .ignoreUnknownCharacters) else { invalid("base64 içerik çözülemedi"); return true }
|
|
472
|
+
data = decoded
|
|
473
|
+
} else if let uri = string("uri"), let url = URL(string: uri), url.isFileURL, let contents = try? Data(contentsOf: url) {
|
|
474
|
+
data = contents
|
|
475
|
+
} else {
|
|
476
|
+
invalid("base64 veya okunabilir bir file:// uri gerekli"); return true
|
|
477
|
+
}
|
|
478
|
+
inbox(reply) { try await Pushizo.Chat.sendAttachment(data, fileName: fileName, mimeType: mimeType, text: text).dictionary }
|
|
479
|
+
case "chatMarkRead":
|
|
480
|
+
let seq = (args["seq"] as? NSNumber)?.intValue ?? 0
|
|
481
|
+
inbox(reply) { ["unreadCount": try await Pushizo.Chat.markRead(seq)] }
|
|
482
|
+
case "chatRefreshUnreadCount":
|
|
483
|
+
inbox(reply) { ["unreadCount": try await Pushizo.Chat.refreshUnreadCount()] }
|
|
484
|
+
case "chatGetUnreadCount": ok(["unreadCount": Pushizo.Chat.unreadCount])
|
|
485
|
+
case "chatLeaveOfflineMessage":
|
|
486
|
+
guard let email = string("email"), let text = string("text") else { invalid("email ve text gerekli"); return true }
|
|
487
|
+
let name = string("name")
|
|
488
|
+
inbox(reply) { try await Pushizo.Chat.leaveOfflineMessage(email: email, text: text, name: name).dictionary }
|
|
489
|
+
case "chatSetTyping": Pushizo.Chat.setTyping(args["typing"] as? Bool ?? false); ok()
|
|
490
|
+
case "chatStartListening": Pushizo.Chat.startListening(); ok()
|
|
491
|
+
case "chatStopListening": Pushizo.Chat.stopListening(); ok()
|
|
492
|
+
case "chatAttachmentUrl":
|
|
493
|
+
guard let attachmentId = string("attachmentId") else { invalid("attachmentId gerekli"); return true }
|
|
494
|
+
let download = args["download"] as? Bool ?? false
|
|
495
|
+
inbox(reply) { ["url": try Pushizo.Chat.attachmentUrl(attachmentId, download: download).absoluteString] }
|
|
496
|
+
case "chatGetVisitorId": ok(["visitorId": Self.orNull(Pushizo.Chat.visitorId)])
|
|
497
|
+
case "chatIsListening": ok(["listening": Pushizo.Chat.isListening])
|
|
498
|
+
// Hazır sohbet ekranı (`HSChatViewController`): etkin sahnenin en üstteki görünüm denetleyicisinden modal açılır.
|
|
499
|
+
case "chatShow":
|
|
500
|
+
guard Pushizo.isInitialized else { reply(.failure(code: "NOT_READY", message: Self.notInitialized)); return true }
|
|
501
|
+
Pushizo.Chat.show(); ok()
|
|
502
|
+
case "chatHide": Pushizo.Chat.hide(); ok()
|
|
503
|
+
// Sunum durumu UIKit'ten okunur: ana iş parçacığında (bekleyen `show`/`hide` işlendikten sonra) yanıtlanır.
|
|
504
|
+
case "chatIsPresented":
|
|
505
|
+
DispatchQueue.main.async { ok(["presented": Pushizo.Chat.isPresented]) }
|
|
506
|
+
|
|
507
|
+
// Live Activities (ADR-0024): genel `PushizoDefaultLiveActivityAttributes` türü; değer doğrulaması yerel SDK'dadır.
|
|
508
|
+
case "setupDefaultLiveActivity":
|
|
509
|
+
liveActivity(reply) { try Pushizo.LiveActivities.setupDefaultLiveActivity() }
|
|
510
|
+
case "startDefaultLiveActivity":
|
|
511
|
+
guard let activityId = string("activityId") else { invalid("activityId gerekli"); return true }
|
|
512
|
+
guard let attributes = args["attributes"] as? [String: Any], let contentState = args["contentState"] as? [String: Any] else {
|
|
513
|
+
invalid("attributes ve contentState nesne olmalı"); return true
|
|
514
|
+
}
|
|
515
|
+
liveActivity(reply) {
|
|
516
|
+
try Pushizo.LiveActivities.startDefaultLiveActivity(activityId: activityId, attributes: attributes, contentState: contentState)
|
|
517
|
+
}
|
|
518
|
+
default:
|
|
519
|
+
return false
|
|
520
|
+
}
|
|
521
|
+
return true
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/// Bildirim kutusu çağrısı: sonuç ana iş parçacığında döner; hata kodu `HSApiError.code` veya SDK durumu.
|
|
525
|
+
private func inbox(_ reply: @escaping (PushizoBridgeResult) -> Void, _ body: @escaping () async throws -> [String: Any]?) {
|
|
526
|
+
Task {
|
|
527
|
+
do {
|
|
528
|
+
let value = try await body()
|
|
529
|
+
DispatchQueue.main.async { reply(.success(value)) }
|
|
530
|
+
} catch let error as HSApiError {
|
|
531
|
+
DispatchQueue.main.async { reply(.failure(code: error.code, message: error.message)) }
|
|
532
|
+
} catch HSChatError.invalidArgument(let message) {
|
|
533
|
+
DispatchQueue.main.async { reply(.failure(code: "INVALID_ARGUMENT", message: message)) }
|
|
534
|
+
} catch let error as HSError {
|
|
535
|
+
let code: String
|
|
536
|
+
switch error {
|
|
537
|
+
case .invalidArgument: code = "INVALID_ARGUMENT"
|
|
538
|
+
case .invalidResponse: code = "HS_ERROR"
|
|
539
|
+
default: code = "NOT_READY"
|
|
540
|
+
}
|
|
541
|
+
DispatchQueue.main.async { reply(.failure(code: code, message: "\(error)")) }
|
|
542
|
+
} catch {
|
|
543
|
+
DispatchQueue.main.async { reply(.failure(code: "HS_ERROR", message: error.localizedDescription)) }
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/// Live Activity çağrısı: `HSLiveActivityError` kodu ve mesajı aynen iletilir; beklenmeyen hata `LIVE_ACTIVITY_FAILED`.
|
|
549
|
+
private func liveActivity(_ reply: (PushizoBridgeResult) -> Void, _ body: () throws -> Void) {
|
|
550
|
+
do {
|
|
551
|
+
try body()
|
|
552
|
+
reply(.success(nil))
|
|
553
|
+
} catch let error as HSLiveActivityError {
|
|
554
|
+
reply(.failure(code: error.code, message: error.message))
|
|
555
|
+
} catch {
|
|
556
|
+
reply(.failure(code: "LIVE_ACTIVITY_FAILED", message: error.localizedDescription))
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// MARK: Eşleme
|
|
561
|
+
|
|
562
|
+
/// Boş olmayan metin; değilse nil.
|
|
563
|
+
static func nonBlank(_ raw: Any?) -> String? {
|
|
564
|
+
guard let value = raw as? String, !value.trimmingCharacters(in: .whitespaces).isEmpty else { return nil }
|
|
565
|
+
return value
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/// Köprüden gelen düz nesne → metin sözlüğü (`null` değerler atlanır, sayı/boolean metne çevrilir).
|
|
569
|
+
static func stringMap(_ raw: Any?) -> [String: String] {
|
|
570
|
+
(raw as? [String: Any] ?? [:]).compactMapValues { value in
|
|
571
|
+
if value is NSNull { return nil }
|
|
572
|
+
return (value as? String) ?? "\(value)"
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
static func map(_ consent: HSUserConsent) -> [String: Any] {
|
|
577
|
+
[
|
|
578
|
+
"purpose": consent.purpose,
|
|
579
|
+
"granted": consent.granted,
|
|
580
|
+
"source": orNull(consent.source),
|
|
581
|
+
"updatedAt": millis(consent.updatedAt),
|
|
582
|
+
]
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
static func logLevel(_ name: String?) -> HSLog.Level {
|
|
586
|
+
switch name {
|
|
587
|
+
case "none": return HSLog.Level.none
|
|
588
|
+
case "error": return .error
|
|
589
|
+
case "info": return .info
|
|
590
|
+
case "debug": return .debug
|
|
591
|
+
default: return .warn
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
static func granted(_ status: UNAuthorizationStatus) -> Bool { status == .authorized || status == .provisional }
|
|
596
|
+
|
|
597
|
+
static func orNull(_ value: Any?) -> Any { value ?? NSNull() }
|
|
598
|
+
|
|
599
|
+
private static func millis(_ date: Date?) -> Any { date.map { Int64($0.timeIntervalSince1970 * 1000) } ?? NSNull() }
|
|
600
|
+
|
|
601
|
+
static func map(_ n: HSNotification) -> [String: Any] {
|
|
602
|
+
["title": orNull(n.title), "body": orNull(n.body), "url": orNull(n.url), "imageUrl": orNull(n.imageUrl),
|
|
603
|
+
"deliveryId": orNull(n.deliveryId), "messageId": orNull(n.messageId), "data": n.data, "androidNotificationId": NSNull()]
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
static func map(_ s: HSPushSubscriptionState) -> [String: Any] {
|
|
607
|
+
["id": orNull(s.id), "token": orNull(s.token), "optedIn": s.optedIn, "permission": granted(s.permission)]
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
static func map(_ item: HSInboxItem) -> [String: Any] {
|
|
611
|
+
["id": item.id, "messageId": orNull(item.messageId), "title": orNull(item.title), "body": orNull(item.body),
|
|
612
|
+
"url": orNull(item.url), "imageUrl": orNull(item.imageUrl), "data": item.data, "read": item.read,
|
|
613
|
+
"readAt": millis(item.readAt), "archivedAt": millis(item.archivedAt), "expiresAt": millis(item.expiresAt), "createdAt": millis(item.createdAt)]
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
static func map(_ p: HSPreference) -> [String: Any] {
|
|
617
|
+
["category": p.category, "channel": orNull(p.channel), "optedIn": p.optedIn, "explicit": p.explicit,
|
|
618
|
+
"source": orNull(p.source), "updatedAt": millis(p.updatedAt)]
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
static func map(_ c: HSMessageCategory) -> [String: Any] {
|
|
622
|
+
["key": c.key, "name": c.name, "description": c.description, "channels": orNull(c.channels), "defaultOptedIn": c.defaultOptedIn]
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
static func map(_ r: HSPreferencesResult) -> [String: Any] {
|
|
626
|
+
["preferences": r.preferences.map { map($0) }, "categories": r.categories.map { map($0) }]
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
static func map(_ card: HSContentCard) -> [String: Any] {
|
|
630
|
+
["id": card.id, "messageId": orNull(card.messageId), "type": card.type.rawValue, "title": orNull(card.title),
|
|
631
|
+
"body": orNull(card.body), "url": orNull(card.url), "imageUrl": orNull(card.imageUrl), "buttonText": orNull(card.buttonText),
|
|
632
|
+
"data": card.data, "pinned": card.pinned, "priority": card.priority, "viewed": card.viewed, "clicked": card.clicked,
|
|
633
|
+
"expiresAt": millis(card.expiresAt), "createdAt": millis(card.createdAt)]
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
static func map(_ feed: HSContentCardFeed) -> [String: Any] {
|
|
637
|
+
["cards": feed.cards.map { map($0) }, "unviewedCount": feed.unviewedCount]
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
static func map(_ page: HSInboxPage) -> [String: Any] {
|
|
641
|
+
["items": page.items.map { map($0) }, "unreadCount": page.unreadCount, "nextCursor": orNull(page.nextCursor), "hasMore": page.hasMore]
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// In-app mesaj modeli → JS `InAppMessage` (alanlar yerel modelle aynı; zamanlar epoch milisaniye).
|
|
645
|
+
static func map(_ m: HSInAppMessage) -> [String: Any] {
|
|
646
|
+
["id": m.id, "priority": m.priority,
|
|
647
|
+
"triggers": m.triggers.map { map($0) }, "triggerGroups": m.triggerGroups.map { group in group.map { map($0) } },
|
|
648
|
+
"variantId": orNull(m.variantId), "display": map(m.display),
|
|
649
|
+
"remainingImpressions": m.remainingImpressions, "minIntervalSeconds": m.minIntervalSeconds,
|
|
650
|
+
"stopAfterInteraction": m.stopAfterInteraction, "lastImpressionAt": millis(m.lastImpressionAt), "endAt": millis(m.endAt),
|
|
651
|
+
"language": orNull(m.language)]
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
static func map(_ d: HSInAppDisplay) -> [String: Any] {
|
|
655
|
+
["layout": d.layout.rawValue, "backgroundColor": orNull(d.backgroundColor), "textColor": orNull(d.textColor),
|
|
656
|
+
"dismissible": d.dismissible, "autoDismissSeconds": orNull(d.autoDismissSeconds), "contentType": d.contentType.rawValue,
|
|
657
|
+
"blocks": d.blocks.map { map($0) }, "html": orNull(d.html), "htmlHeight": orNull(d.htmlHeight)]
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
static func map(_ t: HSInAppTrigger) -> [String: Any] {
|
|
661
|
+
switch t {
|
|
662
|
+
case .sessionStart: return ["kind": "session_start"]
|
|
663
|
+
case let .event(name): return ["kind": "event", "name": name]
|
|
664
|
+
case let .screen(name): return ["kind": "screen", "name": name]
|
|
665
|
+
case let .condition(key, op, value):
|
|
666
|
+
let raw: Any
|
|
667
|
+
switch value {
|
|
668
|
+
case .string(let s)?: raw = s
|
|
669
|
+
case .number(let n)?: raw = n
|
|
670
|
+
case nil: raw = NSNull()
|
|
671
|
+
}
|
|
672
|
+
return ["kind": "trigger", "key": key, "op": op, "value": raw]
|
|
673
|
+
case let .sessionDuration(seconds): return ["kind": "session_duration", "seconds": seconds]
|
|
674
|
+
case let .unsupported(kind): return ["kind": "unsupported", "name": kind]
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
static func map(_ a: HSInAppAction) -> [String: Any] {
|
|
679
|
+
switch a {
|
|
680
|
+
case .dismiss: return ["type": "dismiss"]
|
|
681
|
+
case let .url(url): return ["type": "url", "url": url]
|
|
682
|
+
case let .deeplink(url): return ["type": "deeplink", "url": url]
|
|
683
|
+
case let .tags(tags): return ["type": "tags", "tags": tags]
|
|
684
|
+
case .pushPrompt: return ["type": "push_prompt"]
|
|
685
|
+
case let .outcome(name, value): return ["type": "outcome", "name": name, "value": orNull(value)]
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
static func map(_ b: HSInAppButton) -> [String: Any] {
|
|
690
|
+
["id": b.id, "text": b.text, "action": map(b.action), "backgroundColor": orNull(b.backgroundColor), "textColor": orNull(b.textColor)]
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
static func map(_ c: HSInAppCarouselCard) -> [String: Any] {
|
|
694
|
+
["id": c.id, "title": c.title, "body": orNull(c.body), "imageUrl": orNull(c.imageUrl), "imageAlt": orNull(c.imageAlt),
|
|
695
|
+
"button": c.button.map { map($0) } ?? NSNull()]
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
static func map(_ block: HSInAppBlock) -> [String: Any] {
|
|
699
|
+
switch block {
|
|
700
|
+
case let .image(url, alt): return ["type": "image", "url": url, "alt": orNull(alt)]
|
|
701
|
+
case let .title(text): return ["type": "title", "text": text]
|
|
702
|
+
case let .body(text): return ["type": "body", "text": text]
|
|
703
|
+
case let .button(button):
|
|
704
|
+
var json = map(button)
|
|
705
|
+
json["type"] = "button"
|
|
706
|
+
return json
|
|
707
|
+
case let .carousel(carousel): return ["type": "carousel", "id": carousel.id, "cards": carousel.cards.map { map($0) }]
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/// In-app yaşam döngüsünü etkin çekirdeğe yönlendirir (SDK dinleyiciyi zayıf değil güçlü tutar; tek örnek yeterli).
|
|
713
|
+
final class PushizoBridgeLifecycleForwarder: HSInAppLifecycleListener {
|
|
714
|
+
static let shared = PushizoBridgeLifecycleForwarder()
|
|
715
|
+
func onWillDisplay(_ message: HSInAppMessage) { PushizoBridgeCore.active?.emitLifecycle("willDisplay", message) }
|
|
716
|
+
func onDidDisplay(_ message: HSInAppMessage) { PushizoBridgeCore.active?.emitLifecycle("didDisplay", message) }
|
|
717
|
+
func onWillDismiss(_ message: HSInAppMessage) { PushizoBridgeCore.active?.emitLifecycle("willDismiss", message) }
|
|
718
|
+
func onDidDismiss(_ message: HSInAppMessage) { PushizoBridgeCore.active?.emitLifecycle("didDismiss", message) }
|
|
719
|
+
}
|