• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Badging for PWA app icons, Like Native Apps

PWA Badge

Like Native Apps, The PWA Badge API allows installed web apps to set an application-wide badge on the app icon.

Travis CI codecov CodeFactor GitHub license PRs Welcome All Contributors


The App Badge API allows installed web apps to set an application-wide badge, shown in an operating-system-specific place associated with the application (such as the shelf or home screen).

Badges tend to be more user-friendly than notifications and can be updated with a much higher frequency since they don't interrupt the user. And, because they don't interrupt the user, they don't need the user's permission.

Keep in mind that to display the Badge count, Your PWA application should be installed on your Device.

Possible use cases

Examples of sites that may use this Library includes:

  • Chat, email, and social apps, to signal that new messages have arrived, or to show the number of unread items.
  • Productivity apps, to signal that a long-running background task (such as rendering an image or video) has completed.
  • Games, to signal that a player action is required (e.g., in Chess, when it is the player's turn).

Usage

npm install --save pwa-badge

The Badge API consists of five methods:

  • isSupported() Check if the User's browser supports the Feature, and returns a boolean value that represents the Status of supporting.
  • syncSetBadge(unreadCount) Removes app's badge Synchronously. If a value is provided, set the badge to the provided value otherwise, display a plain white dot (or other flag as appropriate to the platform). Setting number to 0 is the same as calling syncClearBadge() or asyncClearBadge().
  • syncClearBadge() Removes app's badge Synchronously.
  • asyncSetBadge(unreadCount) This API is the same as syncSetBadge() but returns an empty Promise for error handling.
  • asyncClearBadge() Removes app's badge Asynchronously and returns an empty Promise for error handling.

Check Browser supports the Badge API

TL;DR isSupported() method function is an util for informing your users that this feature supports by their Browser or OS and the pwa-badge library set and clear the Badge count safely, and you can avoid using isSupported() before calling the set or clear methods.

import PWABadge from 'pwa-badge';

// Create an Instance
const badge = new PWABadge();

if (badge.isSupported()) {
  // Hoora!, Supports the Badge feature
} else {
  // Does not supports
}

Sync Set and Clear Badge

import PWABadge from 'pwa-badge';

// Create an Instance
const badge = new PWABadge();

// Set Badge unreadCount
badge.syncSetBadge(1);

// Clear Badge unreadCount
badge.syncClearBadge();

Result by calling syncSetBadge:

Async Set and Clear Badge

asyncSetBadge() and asyncClearBadge() return empty promises you can use for error handling.

import PWABadge from 'pwa-badge';

// Create an Instance
const badge = new PWABadge();

// Set Badge unreadCount
badge
  .asyncSetBadge(1)
  .then(() => {
    // Badge count has shown as well
  })
  .catch((e) => {
    // The Browser not supporting the Badge feature or something went wrong
  });

// Clear Badge unreadCount
badge
  .asyncClearBadge()
  .then(() => {
    // Badge count has disappeared
  })
  .catch((e) => {
    // The Browser not supporting the Badge feature or something went wrong
  });

The App Badge API works on Windows, and macOS, in Chrome 81 or later. It has also been confirmed to work on Edge 84 or later. Support for Chrome OS is in development and will be available in a future release of Chrome. On Android, the Badge API is not supported. Instead, Android automatically shows a badge on app icon for the installed web app when there is an unread notification, just as for Android apps.

Some user agents may take a number like 4000 and rewrite it as 99+. If you saturate the badge yourself (for example by setting it to 99) then the + won't appear. No matter the actual number, just call syncSetBadge() or asyncSetBadge() and let the user agent deal with displaying it accordingly.

While the App Badge API in Chrome requires an installed app as I wrote before, you shouldn't make calls to the Badge API dependent on the installation state. Just call the API when it exists and installed on a device, as other browsers may show the badge in other places. If it works, it works. If not, it simply doesn't.

More Repositories

1

react-typescript-hooks-sample

A sample project with React by typescript, hooks and context
JavaScript
35
star
2

billify

Iranian Bill Detector
JavaScript
14
star
3

redux-tehranjs

Redux workshop in Tehranjs.net
JavaScript
8
star
4

intl-combiner

Extract and Combine react-intl messages with together on single locale file.
JavaScript
7
star
5

redux-from-scratch

A simple Redux from scratch based on Redux official code base
TypeScript
7
star
6

nodejs-haproxy

Simple High availability load balancer in NodeJS with HaProxy
JavaScript
5
star
7

falaki-time

Calculate hours constellation
JavaScript
4
star
8

sample-typescript-http-service

TypeScript
3
star
9

keyCode

press any key to get the JavaScript event keycode.
JavaScript
3
star
10

js-interview-questions

Essential javascript interview questions
JavaScript
2
star
11

calculator-game

A calculator game
JavaScript
2
star
12

vue-persian-tools

Persian Tools for Vue.js
JavaScript
2
star
13

devops

Useful templates for running the traefik and portainer docker containers with settings
1
star
14

mapsa-frontend13

Mapsa Frontend 13
JavaScript
1
star
15

react-sample-book-store

JavaScript
1
star
16

MCollection

1
star
17

medwing-frontend

The front-end part of the Medwing coding challenge
JavaScript
1
star
18

obsmet

OBSMET project
JavaScript
1
star
19

cattell

Cattell test answer sheet by react v16 and antd ui framework
JavaScript
1
star
20

ios-password-field-crashes

IOS 14 Keychain bug that crashes App when clicking on Password fields
1
star
21

medwing-backend

The Back-end part of the Medwing coding challenge
JavaScript
1
star
22

colors

A simple tool for converting HEX values to RGB and vice versa.
JavaScript
1
star
23

rahoor

Rahoor workshop
JavaScript
1
star