All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.react-utils.BrowserPushNotifications.pushHelper.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
// THIS FILE HAS BEEN ADDED BY NEETO NOTIFICATIONS ENGINE
// AND IS USED FOR BROWSER NOTIFICATIONS
// CONTACT NEETO NOTIFICATIONS TEAM BEFORE MODIFYING THIS FILE

import devicesApi from "./devices";
import { unsubscribeBrowserNotifications, registerServiceWorker } from "./registerServiceWorker";
var createBrowserSubscription = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(subscription) {
    var payload;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!subscription) {
            _context.next = 13;
            break;
          }
          payload = {
            device: {
              subscription: subscription,
              platform: "browser"
            }
          };
          _context.prev = 2;
          _context.next = 5;
          return devicesApi.create(payload);
        case 5:
          localStorage.setItem("deviceId", JSON.stringify(subscription.toJSON().keys.auth));
          _context.next = 11;
          break;
        case 8:
          _context.prev = 8;
          _context.t0 = _context["catch"](2);
          logger.error("Device registration Failed: ", _context.t0);
        case 11:
          _context.next = 14;
          break;
        case 13:
          logger.error("Something went wrong while registering device for browser subscription");
        case 14:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[2, 8]]);
  }));
  return function createBrowserSubscription(_x) {
    return _ref.apply(this, arguments);
  };
}();
export var destroyBrowserSubscription = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var deviceId;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.prev = 0;
          _context2.next = 3;
          return unsubscribeBrowserNotifications();
        case 3:
          deviceId = JSON.parse(localStorage.getItem("deviceId"));
          _context2.next = 6;
          return devicesApi.destroy(deviceId);
        case 6:
          localStorage.removeItem("deviceId");
          _context2.next = 12;
          break;
        case 9:
          _context2.prev = 9;
          _context2.t0 = _context2["catch"](0);
          logger.error("Something went wrong while unsubscribing browser notification:", _context2.t0);
        case 12:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[0, 9]]);
  }));
  return function destroyBrowserSubscription() {
    return _ref2.apply(this, arguments);
  };
}();
export var registerBrowserNotifications = /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var vapidPublicKey,
      response,
      _args3 = arguments;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          vapidPublicKey = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : process.env.VAPID_PUBLIC_KEY;
          _context3.next = 3;
          return registerServiceWorker(vapidPublicKey);
        case 3:
          response = _context3.sent;
          _context3.next = 6;
          return createBrowserSubscription(response);
        case 6:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return function registerBrowserNotifications() {
    return _ref3.apply(this, arguments);
  };
}();
//# sourceMappingURL=pushHelper.js.map




© 2015 - 2024 Weber Informatics LLC | Privacy Policy