@altcraft/react-native-sdk 0.0.3 → 0.0.4
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/README.md +119 -30
- package/Sdk.podspec +2 -2
- package/android/build.gradle +2 -2
- package/android/src/main/java/com/altcraft/sdk/SdkEventEmitter.kt +50 -0
- package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +122 -12
- package/android/src/main/java/com/altcraft/sdk/SdkPackage.kt +16 -8
- package/ios/Converter.swift +2 -2
- package/ios/Sdk.mm +113 -0
- package/ios/SdkModule.swift +137 -30
- package/lib/module/NativeSdk.js.map +1 -1
- package/lib/module/Utilities.js +84 -0
- package/lib/module/Utilities.js.map +1 -1
- package/lib/module/index.js +141 -29
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeSdk.d.ts +18 -2
- package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
- package/lib/typescript/src/Utilities.d.ts +32 -0
- package/lib/typescript/src/Utilities.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +61 -5
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/NativeSdk.ts +34 -2
- package/src/Utilities.ts +66 -2
- package/src/index.tsx +169 -41
package/src/index.tsx
CHANGED
|
@@ -14,7 +14,7 @@ import NativeSDK, {
|
|
|
14
14
|
type SdkEvent,
|
|
15
15
|
} from './NativeSdk';
|
|
16
16
|
|
|
17
|
-
import { Utilities } from './Utilities';
|
|
17
|
+
import { Utilities, ActionFieldBuilder, actionField } from './Utilities';
|
|
18
18
|
|
|
19
19
|
// ---------------- SDK API ----------------
|
|
20
20
|
|
|
@@ -182,7 +182,10 @@ export function getPushToken(): Promise<TokenData | null> {
|
|
|
182
182
|
* @param token Token string or `null` to clear.
|
|
183
183
|
* @returns Promise that resolves when the token is saved on native side.
|
|
184
184
|
*/
|
|
185
|
-
export function setPushToken(
|
|
185
|
+
export function setPushToken(
|
|
186
|
+
provider: string,
|
|
187
|
+
token: string | null
|
|
188
|
+
): Promise<void> {
|
|
186
189
|
return NativeSDK.setPushToken(provider, token);
|
|
187
190
|
}
|
|
188
191
|
|
|
@@ -197,7 +200,6 @@ export function clear(): Promise<void> {
|
|
|
197
200
|
return NativeSDK.clear();
|
|
198
201
|
}
|
|
199
202
|
|
|
200
|
-
|
|
201
203
|
// ---------------- MobileEvent ----------------
|
|
202
204
|
|
|
203
205
|
/**
|
|
@@ -233,7 +235,7 @@ export function mobileEvent(
|
|
|
233
235
|
matchingType,
|
|
234
236
|
Utilities.toNativeStringMap(profileFields),
|
|
235
237
|
Utilities.subscriptionToNativeMap(subscription),
|
|
236
|
-
utm
|
|
238
|
+
utm ?? { campaign: null, content: null, keyword: null, medium: null, source: null, temp: null }
|
|
237
239
|
);
|
|
238
240
|
}
|
|
239
241
|
|
|
@@ -244,54 +246,66 @@ const ALTCRAFT_SDK_EVENT_NAME = 'AltcraftSdkEvent' as const;
|
|
|
244
246
|
const nativeEventEmitter = new NativeEventEmitter(
|
|
245
247
|
NativeModules.SDKEventEmitter
|
|
246
248
|
);
|
|
249
|
+
|
|
247
250
|
let eventsSubscription: EventsSubscription = null;
|
|
251
|
+
const eventHandlers = new Set<(event: SdkEvent) => void>();
|
|
252
|
+
|
|
253
|
+
function ensureNativeSubscription(): void {
|
|
254
|
+
if (eventsSubscription === null) {
|
|
255
|
+
NativeSDK.subscribeToEvents();
|
|
256
|
+
eventsSubscription = nativeEventEmitter.addListener(
|
|
257
|
+
ALTCRAFT_SDK_EVENT_NAME,
|
|
258
|
+
(...args: unknown[]) => {
|
|
259
|
+
const event = (args[0] ?? null) as SdkEvent | null;
|
|
260
|
+
if (!event) return;
|
|
261
|
+
|
|
262
|
+
const handlers = Array.from(eventHandlers);
|
|
263
|
+
for (const handler of handlers) {
|
|
264
|
+
try {
|
|
265
|
+
handler(event);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
console.warn('[AltcraftSDK] subscribeToEvents handler error:', e);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
248
274
|
|
|
249
275
|
/**
|
|
250
276
|
* Subscribes to native SDK events and routes them to the provided handler.
|
|
251
277
|
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
278
|
+
* Multiple callers can subscribe independently — each handler receives every event.
|
|
279
|
+
* The native subscription is activated only once (first caller).
|
|
254
280
|
*
|
|
255
281
|
* @param handler Callback invoked for each incoming SDK event.
|
|
256
282
|
* The event object is produced by native and follows {@link SdkEvent}.
|
|
257
283
|
*/
|
|
258
284
|
export function subscribeToEvents(handler: (event: SdkEvent) => void): void {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
eventsSubscription = null;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Enables native-side subscription (starts producing events).
|
|
265
|
-
NativeSDK.subscribeToEvents();
|
|
266
|
-
|
|
267
|
-
eventsSubscription = nativeEventEmitter.addListener(
|
|
268
|
-
ALTCRAFT_SDK_EVENT_NAME,
|
|
269
|
-
(...args: unknown[]) => {
|
|
270
|
-
const event = (args[0] ?? null) as SdkEvent | null;
|
|
271
|
-
if (!event) return;
|
|
272
|
-
|
|
273
|
-
try {
|
|
274
|
-
handler(event);
|
|
275
|
-
} catch (e) {
|
|
276
|
-
console.warn('[AltcraftSDK] subscribeToEvents handler error:', e);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
);
|
|
285
|
+
eventHandlers.add(handler);
|
|
286
|
+
ensureNativeSubscription();
|
|
280
287
|
}
|
|
281
288
|
|
|
282
289
|
/**
|
|
283
|
-
* Unsubscribes from native SDK events.
|
|
290
|
+
* Unsubscribes a specific handler from native SDK events.
|
|
284
291
|
*
|
|
285
|
-
*
|
|
292
|
+
* When the last handler is removed, the native subscription is also released.
|
|
286
293
|
* Safe to call even if not subscribed.
|
|
287
294
|
*/
|
|
288
|
-
export function unsubscribeFromEvent(
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
295
|
+
export function unsubscribeFromEvent(
|
|
296
|
+
handler?: (event: SdkEvent) => void
|
|
297
|
+
): void {
|
|
298
|
+
if (handler) {
|
|
299
|
+
eventHandlers.delete(handler);
|
|
292
300
|
}
|
|
293
301
|
|
|
294
|
-
|
|
302
|
+
if (eventHandlers.size === 0) {
|
|
303
|
+
if (eventsSubscription) {
|
|
304
|
+
eventsSubscription.remove();
|
|
305
|
+
eventsSubscription = null;
|
|
306
|
+
}
|
|
307
|
+
NativeSDK.unsubscribeFromEvent();
|
|
308
|
+
}
|
|
295
309
|
}
|
|
296
310
|
|
|
297
311
|
//Android-only behavior
|
|
@@ -310,7 +324,7 @@ export function takePush(message: Record<string, string>): void {
|
|
|
310
324
|
|
|
311
325
|
/**
|
|
312
326
|
* Requests notification permission (Android-only behavior).
|
|
313
|
-
* On Android it may trigger runtime permission flow,
|
|
327
|
+
* On Android it may trigger runtime permission flow,
|
|
314
328
|
* n iOS it can be a no-op or routed to native implementation.
|
|
315
329
|
*/
|
|
316
330
|
export function requestNotificationPermission(): void {
|
|
@@ -332,9 +346,14 @@ export function deliveryEvent(
|
|
|
332
346
|
const nativePayload =
|
|
333
347
|
message && Object.keys(message).length > 0 ? { ...message } : null;
|
|
334
348
|
|
|
335
|
-
(
|
|
336
|
-
|
|
337
|
-
|
|
349
|
+
(
|
|
350
|
+
NativeSDK as unknown as {
|
|
351
|
+
deliveryEvent: (
|
|
352
|
+
m: Record<string, string> | null,
|
|
353
|
+
uid: string | null
|
|
354
|
+
) => void;
|
|
355
|
+
}
|
|
356
|
+
).deliveryEvent(nativePayload, messageUID);
|
|
338
357
|
}
|
|
339
358
|
|
|
340
359
|
/**
|
|
@@ -350,9 +369,11 @@ export function openEvent(
|
|
|
350
369
|
const nativePayload =
|
|
351
370
|
message && Object.keys(message).length > 0 ? { ...message } : null;
|
|
352
371
|
|
|
353
|
-
(
|
|
354
|
-
|
|
355
|
-
|
|
372
|
+
(
|
|
373
|
+
NativeSDK as unknown as {
|
|
374
|
+
openEvent: (m: Record<string, string> | null, uid: string | null) => void;
|
|
375
|
+
}
|
|
376
|
+
).openEvent(nativePayload, messageUID);
|
|
356
377
|
}
|
|
357
378
|
|
|
358
379
|
// ---------------- UserDefaults ----------------
|
|
@@ -372,6 +393,96 @@ export function setUserDefaultsValue(
|
|
|
372
393
|
return NativeSDK.setUserDefaultsValue(suiteName, key, str);
|
|
373
394
|
}
|
|
374
395
|
|
|
396
|
+
// ---------------- Profile API ----------------
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Updates profile fields on the server.
|
|
400
|
+
*
|
|
401
|
+
* @param profileFields Optional profile fields to update.
|
|
402
|
+
* @param skipTriggers Optional flag to skip trigger execution on the server.
|
|
403
|
+
*/
|
|
404
|
+
export function updateProfileFields(
|
|
405
|
+
profileFields: Record<string, unknown> | null = null,
|
|
406
|
+
skipTriggers: boolean | null = null
|
|
407
|
+
): void {
|
|
408
|
+
NativeSDK.updateProfileFields(
|
|
409
|
+
Utilities.toNativeStringMap(profileFields),
|
|
410
|
+
skipTriggers
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ---------------- Token management API ----------------
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Forces a push token refresh (delete current token + re-register).
|
|
418
|
+
*
|
|
419
|
+
* @returns Promise that resolves when the token update flow completes.
|
|
420
|
+
*/
|
|
421
|
+
export function forcedTokenUpdate(): Promise<void> {
|
|
422
|
+
return NativeSDK.forcedTokenUpdate();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Deletes the device token for a given provider.
|
|
427
|
+
*
|
|
428
|
+
* @param provider Provider identifier (e.g. `android-firebase`, `ios-firebase`).
|
|
429
|
+
* @returns Promise that resolves when the delete call completes.
|
|
430
|
+
*/
|
|
431
|
+
export function deleteDeviceToken(provider: string): Promise<void> {
|
|
432
|
+
return NativeSDK.deleteDeviceToken(provider);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Changes the push provider priority list at runtime.
|
|
437
|
+
*
|
|
438
|
+
* @param priorityList Ordered list of provider identifiers.
|
|
439
|
+
* @returns Promise that resolves when the priority list is updated.
|
|
440
|
+
*/
|
|
441
|
+
export function changePushProviderPriorityList(
|
|
442
|
+
priorityList: string[] | null
|
|
443
|
+
): Promise<void> {
|
|
444
|
+
if (!priorityList || priorityList.length === 0) {
|
|
445
|
+
return Promise.resolve();
|
|
446
|
+
}
|
|
447
|
+
return NativeSDK.changePushProviderPriorityList(priorityList);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// ---------------- PushReceiver helpers ----------------
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Checks whether a push payload belongs to Altcraft.
|
|
454
|
+
*
|
|
455
|
+
* @param message Push payload map (string-only).
|
|
456
|
+
* @returns `true` if the message contains the Altcraft marker key.
|
|
457
|
+
*/
|
|
458
|
+
export function isAltcraftPush(message: Record<string, string>): boolean {
|
|
459
|
+
if (!message || Object.keys(message).length === 0) return false;
|
|
460
|
+
return NativeSDK.isAltcraftPush({ ...message });
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// ---------------- SDK lifecycle ----------------
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Allows re-running initial retry/scheduling/token-check logic
|
|
467
|
+
* in this process on the next initialization (Android only).
|
|
468
|
+
* No-op on iOS.
|
|
469
|
+
*/
|
|
470
|
+
export function unlockInitialOperationsInThisSession(): void {
|
|
471
|
+
NativeSDK.unlockInitialOperationsInThisSession();
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// ---------------- iOS-only ----------------
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Sets the App Group name for shared UserDefaults and Core Data (iOS only).
|
|
478
|
+
* Must be called before SDK initialization.
|
|
479
|
+
*
|
|
480
|
+
* @param groupName App Group identifier or `null` to clear.
|
|
481
|
+
*/
|
|
482
|
+
export function setAppGroup(groupName: string | null): void {
|
|
483
|
+
NativeSDK.setAppGroup(groupName);
|
|
484
|
+
}
|
|
485
|
+
|
|
375
486
|
// ---------------- Default export ----------------
|
|
376
487
|
|
|
377
488
|
/**
|
|
@@ -423,10 +534,27 @@ const AltcraftSDK = {
|
|
|
423
534
|
deliveryEvent,
|
|
424
535
|
openEvent,
|
|
425
536
|
requestNotificationPermission,
|
|
537
|
+
unlockInitialOperationsInThisSession,
|
|
538
|
+
|
|
539
|
+
// Profile API
|
|
540
|
+
updateProfileFields,
|
|
541
|
+
|
|
542
|
+
// Token management
|
|
543
|
+
forcedTokenUpdate,
|
|
544
|
+
deleteDeviceToken,
|
|
545
|
+
changePushProviderPriorityList,
|
|
546
|
+
|
|
547
|
+
// PushReceiver helpers
|
|
548
|
+
isAltcraftPush,
|
|
549
|
+
|
|
550
|
+
// iOS only
|
|
551
|
+
setAppGroup,
|
|
426
552
|
};
|
|
427
553
|
|
|
428
554
|
export default AltcraftSDK;
|
|
429
555
|
|
|
556
|
+
export { ActionFieldBuilder, actionField };
|
|
557
|
+
|
|
430
558
|
export type {
|
|
431
559
|
AltcraftConfig,
|
|
432
560
|
CategoryData,
|