@cloudsignal/pwa-sdk 2.0.0 → 2.1.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/dist/index.js CHANGED
@@ -1029,6 +1029,15 @@ function removeRegistrationId(organizationId, serviceId) {
1029
1029
  const key = `registration_${organizationId}_${serviceId}`;
1030
1030
  return removeStorageItem(key);
1031
1031
  }
1032
+ function isInstallationRegistered(organizationId, serviceId) {
1033
+ const key = `install_registered_${organizationId}_${serviceId}`;
1034
+ return getStorageItem(key) === true;
1035
+ }
1036
+ function markInstallationRegistered(organizationId, serviceId, registrationId) {
1037
+ const key = `install_registered_${organizationId}_${serviceId}`;
1038
+ setStorageItem(`install_id_${organizationId}_${serviceId}`, registrationId);
1039
+ return setStorageItem(key, true);
1040
+ }
1032
1041
  var IndexedDBStorage = class {
1033
1042
  constructor(dbName = "CloudSignalPWA", dbVersion = 1) {
1034
1043
  this.db = null;
@@ -1361,6 +1370,64 @@ var PushNotificationManager = class {
1361
1370
  return null;
1362
1371
  }
1363
1372
  }
1373
+ /**
1374
+ * Register a PWA installation without a push subscription.
1375
+ *
1376
+ * Tracks users who installed the PWA (standalone display mode) but
1377
+ * haven't granted notification permission yet. A subsequent
1378
+ * ``registerForPush()`` call upgrades the same row to a full push
1379
+ * registration (matched server-side by browser fingerprint).
1380
+ *
1381
+ * Guarded by a localStorage flag so repeat ``initialize()`` calls in
1382
+ * the same storage session are no-ops.
1383
+ */
1384
+ async registerInstallation() {
1385
+ try {
1386
+ if (isInstallationRegistered(this.organizationId, this.serviceId)) {
1387
+ this.log("Installation already registered, skipping");
1388
+ return null;
1389
+ }
1390
+ const fingerprint = await generateBrowserFingerprint();
1391
+ const deviceInfo = this.deviceDetector.getDeviceInfo();
1392
+ const platformInfo = this.deviceDetector.getPlatformInfo();
1393
+ const installationData = {
1394
+ service_id: this.serviceId,
1395
+ browser_fingerprint: fingerprint || void 0,
1396
+ device_type: deviceInfo.deviceType,
1397
+ device_model: deviceInfo.deviceModel,
1398
+ browser_name: platformInfo.browser,
1399
+ browser_version: platformInfo.browserVersion,
1400
+ os_name: platformInfo.os,
1401
+ os_version: platformInfo.osVersion,
1402
+ user_agent: platformInfo.userAgent,
1403
+ display_mode: "standalone",
1404
+ is_installed: true,
1405
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
1406
+ language: navigator.language || "en-US"
1407
+ };
1408
+ const url = `${this.serviceUrl}/api/v1/registration/install-only`;
1409
+ const response = await makeAuthenticatedRequestWithContext(
1410
+ this.authContext,
1411
+ "POST",
1412
+ url,
1413
+ installationData,
1414
+ this.onTokenExpired
1415
+ );
1416
+ if (!response.ok) {
1417
+ const errorText = await response.text();
1418
+ throw new Error(`Installation registration failed: ${response.status} - ${errorText}`);
1419
+ }
1420
+ const result = await response.json();
1421
+ markInstallationRegistered(this.organizationId, this.serviceId, result.registration_id);
1422
+ this.log(`Installation registered: ${result.registration_id}`);
1423
+ return { registrationId: result.registration_id };
1424
+ } catch (error) {
1425
+ const err = error instanceof Error ? error : new Error(String(error));
1426
+ this.log(`Installation registration failed: ${err.message}`, "error");
1427
+ this.onError?.(err);
1428
+ return null;
1429
+ }
1430
+ }
1364
1431
  /**
1365
1432
  * Unregister from push notifications
1366
1433
  */
@@ -3145,11 +3212,19 @@ var CloudSignalPWA = class {
3145
3212
  this.configureNotificationAnalytics(swReg);
3146
3213
  }
3147
3214
  if (this.iosInstallBanner && this.config.iosInstallBanner?.showOnFirstVisit !== false) {
3148
- const installState = this.installationManager.getState();
3149
- if (!installState.isInstalled) {
3215
+ const installState2 = this.installationManager.getState();
3216
+ if (!installState2.isInstalled) {
3150
3217
  this.iosInstallBanner.show();
3151
3218
  }
3152
3219
  }
3220
+ const installState = this.installationManager.getState();
3221
+ if (installState.isInstalled) {
3222
+ this.log("PWA installation detected, registering\u2026");
3223
+ const installResult = await this.pushNotificationManager.registerInstallation();
3224
+ if (installResult) {
3225
+ this.emit("install:registered", { registrationId: installResult.registrationId });
3226
+ }
3227
+ }
3153
3228
  this.initialized = true;
3154
3229
  const result = {
3155
3230
  success: true,