UNPKG

@capawesome/capacitor-badge

Version:

Capacitor plugin to access and update the badge number of the app icon.

77 lines (70 loc) 2.2 kB
var capacitorBadge = (function (exports, core) { 'use strict'; const Badge = core.registerPlugin('Badge', { web: () => Promise.resolve().then(function () { return web; }).then(m => new m.BadgeWeb()), }); class BadgeWeb extends core.WebPlugin { constructor() { super(); this.restore(); } async checkPermissions() { return { display: 'granted' }; } async requestPermissions() { return { display: 'granted' }; } async get() { const value = localStorage.getItem(BadgeWeb.STORAGE_KEY); const count = value ? parseInt(value, 10) : 0; return { count }; } async set(options) { const count = options.count; if (count === 0) { await navigator.clearAppBadge(); } else { await navigator.setAppBadge(count); } const value = count.toString(); localStorage.setItem(BadgeWeb.STORAGE_KEY, value); } async increase() { const { count } = await this.get(); await this.set({ count: count + 1 }); } async decrease() { const { count } = await this.get(); if (count < 1) { return; } await this.set({ count: count - 1 }); } async clear() { await this.set({ count: 0 }); } async isSupported() { const result = { isSupported: 'setAppBadge' in navigator, }; return result; } async restore() { const value = localStorage.getItem(BadgeWeb.STORAGE_KEY); if (!value) { return; } const count = parseInt(value, 10); await navigator.setAppBadge(count); } } BadgeWeb.STORAGE_KEY = 'capacitor.badge'; var web = /*#__PURE__*/Object.freeze({ __proto__: null, BadgeWeb: BadgeWeb }); exports.Badge = Badge; return exports; })({}, capacitorExports); //# sourceMappingURL=plugin.js.map