UNPKG

sinch-rtc

Version:

RTC JavaScript/Web SDK

70 lines 3.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.W3CPushWorker = void 0; const StorageFactory_1 = require("../storage/StorageFactory"); const Event_1 = require("../utils/Event"); const PushServiceWorker_1 = require("./PushServiceWorker"); class W3CPushWorker { constructor(serviceWorkerName, pushService) { this.pushService = pushService; this.pushEvent = new Event_1.Event(); this.settings = StorageFactory_1.StorageFactory.createSettingStorage(); this.pushServiceWorker = PushServiceWorker_1.ServiceWorkerFactory.create(serviceWorkerName); } showNotification(title, options) { this.pushServiceWorker.showNotification(title, options); } register() { return __awaiter(this, void 0, void 0, function* () { yield this.pushServiceWorker.register(); this.pushServiceWorker.addEventListener(this.pushEvent); }); } unregister() { return __awaiter(this, void 0, void 0, function* () { yield this.pushServiceWorker.unregister(); this.pushEvent.clean(); }); } addEventListener(action) { this.pushEvent.add(action); } start(applicationServerKey, instance) { return __awaiter(this, void 0, void 0, function* () { yield this.assertPushEnvironment(); let subscription = yield this.pushServiceWorker.getSubscription(); if (!subscription) subscription = yield this.pushServiceWorker.subscribe(applicationServerKey); else if (this.settings.applicationServerKey != applicationServerKey) { // we got a new applicationServerKey so we need to resubscribe yield this.pushServiceWorker.unsubscribe(subscription); subscription = yield this.pushServiceWorker.subscribe(applicationServerKey); } this.settings.applicationServerKey = applicationServerKey; yield this.pushService.registerPushToken(subscription, applicationServerKey, instance); }); } assertPushEnvironment() { return __awaiter(this, void 0, void 0, function* () { if (!this.pushServiceWorker.isPushManagerAvailable()) { throw new Error(W3CPushWorker.ERROR_MANAGER_NOT_AVAILABLE); } if (yield this.pushServiceWorker.arePushPermissionsExplicitlyDenied()) { throw new Error(W3CPushWorker.ERROR_PERMISSIONS_DENIED); } }); } } exports.W3CPushWorker = W3CPushWorker; W3CPushWorker.ERROR_MANAGER_NOT_AVAILABLE = "PushManager not available. Make sure you're using properly configured application on browser that supports push notifications."; W3CPushWorker.ERROR_PERMISSIONS_DENIED = "Push notifications permissions denied. Make sure you allow push notifications."; //# sourceMappingURL=PushWorker.js.map