Tiny Frontend Logo
Posts 0119

On This Page

Sending Notifications

The Notification API allows web developers to create and display desktop notifications to users.

These notifications can provide asynchronous information, even if the user has switched between tabs or moved to another application.

Before Showing Notifications

Before showing notifications, we need to request permission from the user.

The Notification API enforces two security features:

  1. Users must explicitly agree to receive notifications on a per-origin basis.
  2. Only code running in a secure context (HTTPS) can send notifications.

Request Permission

To request permission, use the Notification.requestPermission() method:

let result = await Notification.requestPermission();

The result indicates the current permission to display notifications:

  • denied: The user refuses to have notifications displayed.
  • granted: The user accepts notifications.
  • default: The user's choice is unknown (browser acts as if denied).

If users deny permission explicitly, the browser remembers the choice, and we won't get a second chance to request permission.

If users ignore the request, we can ask again.

Create a Notification

To create a notification, use the Notification constructor.

const notification = new Notification(
  title,
  options
);

There're come custom settings that allow you config the notifications:

  • options.body: A string representing the body text of the notification, which is displayed below the title. The default is the empty string.
  • options.icon: A string containing the URL of an icon to be displayed in the notification.
  • options.image: A string containing the URL of an image to be displayed in the notification.
Subscribe to read the full content

* Free subscribe to enjoy full content access.
* You can cancel anytime with 1-click.

Read Next

0118

Higher-Order Functions (HOF) in JavaScript

In JavaScript, functions are treated as first-class citizens. We can use functions as values, assign them to variables, pass them as arguments to other functions, or even return them from other functions.

Higher-Order Functions (HOF) in JavaScript
0117

JavaScript Closures

This question frequently arises during interviews. If you're unsure, here is everything you should know!

JavaScript Closures
0116

Ordering Flexible Elements

In many cases, we rely on the flex-direction property to manage the display order of flexible items.

Ordering Flexible Elements
0115

JavaScript: Page Location

Whether you're a seasoned developer or just starting your journey, understanding how to manipulate the web page's location through JavaScript can be a very useful tool in your arsenal.

JavaScript: Page Location
0114

Cookies

As a frontend developer, understanding how cookies work is essential for building robust web applications. In this article, we'll explore the world of browser cookies, demystify their mechanics, and learn how to harness their power in your projects.

0113

JavaScript: Local Storage

Understanding how to store data on the client side is essential for creating robust web applications. In this article, we'll explore local storage, one of the key mechanisms for caching data within the user's browser. Let's dive in!