Home

Awesome

⚠️ Deprecated repository

This project has been moved to the following monorepo: capawesome-team/capacitor-plugins.


<p align="center"><br><img src="https://avatars.githubusercontent.com/u/105555861" width="128" height="128" /></p> <h3 align="center">Badge</h3> <p align="center"><strong><code>@capawesome/capacitor-badge</code></strong></p> <p align="center"> Capacitor plugin to access and update the badge number of the app icon. </p> <p align="center"> <img src="https://img.shields.io/maintenance/yes/2023?style=flat-square" /> <a href="https://github.com/capawesome-team/capacitor-badge/actions?query=workflow%3A%22CI%22"><img src="https://img.shields.io/github/actions/workflow/status/capawesome-team/capacitor-badge/ci.yml?branch=main&style=flat-square" /></a> <a href="https://www.npmjs.com/package/@capawesome/capacitor-badge"><img src="https://img.shields.io/npm/l/@capawesome/capacitor-badge?style=flat-square" /></a> <br> <a href="https://www.npmjs.com/package/@capawesome/capacitor-badge"><img src="https://img.shields.io/npm/dw/@capawesome/capacitor-badge?style=flat-square" /></a> <a href="https://www.npmjs.com/package/@capawesome/capacitor-badge"><img src="https://img.shields.io/npm/v/@capawesome/capacitor-badge?style=flat-square" /></a> <a href="https://github.com/capawesome-team"><img src="https://img.shields.io/badge/part%20of-capawesome-%234f46e5?style=flat-square" /></a> </p>

Maintainers

MaintainerGitHubSocial
Robin Genzrobingenz@robin_genz

Sponsors

This is an MIT-licensed open source project. It can grow thanks to the support by these awesome people. If you'd like to join them, please read more here.

<!-- sponsors --><!-- sponsors -->

Installation

npm install @capawesome/capacitor-badge
npx cap sync

Android Variables

This plugin will use the following project variables (defined in your app’s variables.gradle file):

Configuration

<docgen-config> <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

These configuration values are available:

PropTypeDescriptionDefault
persist<code>boolean</code>Configure whether the plugin should restore the counter after a reboot or app restart. Only available for Android and iOS.<code>true</code>
autoClear<code>boolean</code>Configure whether the plugin should reset the counter after resuming the application. Only available for Android and iOS.<code>false</code>

Examples

In capacitor.config.json:

{
  "plugins": {
    "Badge": {
      "persist": true,
      "autoClear": false
    }
  }
}

In capacitor.config.ts:

/// <reference types="@capawesome/capacitor-badge" />

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  plugins: {
    Badge: {
      persist: true,
      autoClear: false,
    },
  },
};

export default config;
</docgen-config>

Demo

A working example can be found here: robingenz/capacitor-plugin-demo

Usage

import { Badge } from '@capawesome/capacitor-badge';

const get = async () => {
  const result = await Badge.get();
  return result.count;
};

const set = async (count: number) => {
  await Badge.set({ count });
};

const increase = async () => {
  await Badge.increase();
};

const decrease = async () => {
  await Badge.decrease();
};

const clear = async () => {
  await Badge.clear();
};

const isSupported = async () => {
  const result = await Badge.isSupported();
  return result.isSupported;
};

const checkPermissions = async () => {
  const result = await Badge.checkPermissions();
};

const requestPermissions = async () => {
  const result = await Badge.requestPermissions();
};

API

<docgen-index> </docgen-index> <docgen-api> <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

get()

get() => Promise<GetBadgeResult>

Get the badge count. The badge count won't be lost after a reboot or app restart.

Default: 0.

Returns: <code>Promise<<a href="#getbadgeresult">GetBadgeResult</a>></code>


set(...)

set(options: SetBadgeOptions) => Promise<void>

Set the badge count.

ParamType
options<code><a href="#setbadgeoptions">SetBadgeOptions</a></code>

increase()

increase() => Promise<void>

Increase the badge count.


decrease()

decrease() => Promise<void>

Decrease the badge count.


clear()

clear() => Promise<void>

Clear the badge count.


isSupported()

isSupported() => Promise<IsSupportedResult>

Check if the badge count is supported.

Returns: <code>Promise<<a href="#issupportedresult">IsSupportedResult</a>></code>


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

Check permission to display badge.

Returns: <code>Promise<<a href="#permissionstatus">PermissionStatus</a>></code>


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

Request permission to display badge.

Returns: <code>Promise<<a href="#permissionstatus">PermissionStatus</a>></code>


Interfaces

GetBadgeResult

PropType
count<code>number</code>

SetBadgeOptions

PropType
count<code>number</code>

IsSupportedResult

PropType
isSupported<code>boolean</code>

PermissionStatus

PropTypeDescription
display<code><a href="#permissionstate">PermissionState</a></code>Permission state of displaying the badge.

Type Aliases

PermissionState

<code>'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'</code>

</docgen-api>

Quirks

On Android not all launchers support badges. This plugin uses ShortcutBadger. All supported launchers are listed there.

On Web, the app must run as an installed PWA (in the taskbar or dock).

Changelog

See CHANGELOG.md.

License

See LICENSE.