Building Progressive Web Apps with H5 Push Notifications
Jul 18, 2025 am 12:43 AMTo enable push notifications in the H5 page, firstly, user authorization is required, and permission is obtained through Notification.requestPermission(); secondly, use PushManager.subscribe() to obtain subscription information, including endpoint, publicKey and authToken; finally, send the subscription information to the backend to save. Push messages are sent by the backend calling the Web Push protocol. For example, in Node.js, you can use the web-push library to construct a request and send a POST to the endpoint address. When the message arrives, the Service Worker listens to the push event and displays the notification through showNotification, and also listens to the notificationclick event to achieve click jump. Cross-platform, Chrome and Firefox support is good. Safari is only available for macOS/iOS. iOS Safari does not support silent push in the background. It is recommended to refresh the subscription regularly and detect the permission status.
Want to use H5 to create an experience similar to native app push notifications? In fact, through Web Push and Service Worker, H5 can also realize the ability to "push messages to user devices". The key is to understand the entire process and correctly configure the relevant services.

How to enable push notifications in the H5 page?
To make a web page push messages, several core steps are required:
- User authorization: The browser will pop up a prompt asking whether to allow notifications to be received
- Get Push subscription information: including endpoint, publicKey, authToken and other fields
- Send subscription information to the backend to save for subsequent push
The key to this step is to use the two APIs: Notification.requestPermission()
and PushManager.subscribe()
. for example:

if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js').then(function(registration) { return registration.pushManager.getSubscription(); }).then(function(subscription) { if (!subscription) { return registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlBase64ToUint8Array('your public key') }); } return subscription; }).then(function(subscription) { // Send subscription to your server}); }
Note: applicationServerKey
is a VAPID public key that is used to verify push permissions.
Where do push messages come from? How to send the backend?
After the front-end gets the subscription information, the back-end can call the Web Push protocol to send messages.

Commonly used libraries are:
- Node.js: web-push
- Python: pywebpush
- PHP: minishlink/web-push
Taking Node.js as an example, the basic process is as follows:
- Fetch the user's Push subscription information from the database (JSON format)
- Use the web-push library to construct request headers and encrypted data bodies
- Initiate a POST request to the endpoint address
- Processing returns status code (such as 404 means that the subscription has expired)
Sample code:
const webPush = require('web-push'); webPush.setVapidDetails( 'mailto:your@email.com', process.env.PUBLIC_KEY, process.env.PRIVATE_KEY ); webPush.sendNotification(subscription, JSON.stringify({ title: 'New news', body: 'This is the push content from the server' }));
If the push fails, remember to clean up the invalid subscription history.
How to display the message after it arrives?
When the push reaches the user device, the Service Worker receives a push
event. You need to listen to this event in sw.js and decide whether to display the notification.
For example:
self.addEventListener('push', event => { const data = event.data.json(); event.waitUntil( self.registration.showNotification(data.title, { body: data.body, icon: '/icon.png', badge: '/badge.png', data: { url: '/' } // Can be used to click to jump}) ); }); self.addEventListener('notificationclick', event => { event.notification.close(); event.waitUntil( clients.openWindow(event.notification.data.url) ); });
This way users can receive notifications and click to open the page just like a native app.
Cross-platform compatibility considerations
The support for Push varies slightly from different systems and browsers:
- Chrome and Firefox support is better, but Safari can only use Apple Push on macOS/iOS
- The performance is stable on mobile Android, while iOS Safari has many restrictions (if it does not support silent push in the background)
- Users may turn off notification permissions or clear the Service Worker cache, causing the subscription to expire
It is recommended to refresh the subscription information regularly and actively detect the current notification permission status when the user accesses.
Basically that's it. Although the process is a bit long, each step is not complicated. The key is to connect the front-end and back-end cooperation.
The above is the detailed content of Building Progressive Web Apps with H5 Push Notifications. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

How to use push notifications in FastAPI to update data in real time Introduction: With the continuous development of the Internet, real-time data updates are becoming more and more important. For example, in application scenarios such as real-time trading, real-time monitoring, and real-time gaming, we need to update data in a timely manner to provide the most accurate information and the best user experience. FastAPI is a modern Python-based web framework that provides a simple and efficient way to build high-performance web applications. This article will introduce how to use FastAPI to implement

Tutorial: Use OneSignal to add push notification function to PHP applications Introduction: With the popularity of smartphones, push notifications have become one of the important means to attract user attention and improve user experience. As a powerful push notification service platform, OneSignal provides developers with a convenient and easy-to-use API, making it easy and fast to add push notification functions to PHP applications. This tutorial will take you through the basic usage of OneSignal and show you how to add push notification functionality to your PHP application. one,

In modern web application development, WebSocket is a common technology for instant communication and real-time data transfer. The SpringBoot framework provides support for integrated WebSocket, making it very convenient for developers to implement push and notification functions. This article will introduce how to use WebSocket to implement push and notification functions in SpringBoot, and demonstrate the implementation of a simple real-time online chat room. Create a SpringBoot project First, we need to create a

ThinkPHP6 sends push notifications: Implementing user message push Introduction: In modern web applications, message push has become one of the important functions to provide real-time notifications and instant updates. Users will receive timely message reminders during the operation, improving user experience and interactivity. This article will introduce how to implement the user message push function in the ThinkPHP6 framework, with code examples. 1. Preparation work: Make sure that the ThinkPHP6 framework has been installed and configured. Install the extension package: composerre

Overview of How to Use Middleware for Push Notifications in Laravel: Push notifications are a common feature in modern applications that allow us to send real-time messages to users to remind them to perform certain actions or update their status. Laravel is a powerful PHP framework that provides middleware features that can help us implement push notification functions. This article will introduce how to use middleware to implement push notifications in Laravel and provide specific code examples. Step 1: Set up push notification driver in

Use the JPush extension to add push notification functionality to PHP applications and quickly implement message push. As one of the common functions of modern applications, message push plays a vital role in user experience and information delivery. In order to implement the push notification function, we can use a third-party service provider like JPush. JPush is a professional push service provider that provides developers with a complete set of APIs and tools to quickly implement message push functions. Below we will use the JPush PHP extension, through

According to news on February 16, open network advocacy organizations have previously questioned Apple’s new European regulations, believing that they severely weakened PWA network applications and reduced them to shortcuts. Apple updated its official support document to state that this move is to comply with the requirements of the Digital Markets Act. Apple explained that after the release of non-WebKit rendering engines in the European market, a new integration architecture must be established. However, the current iOS system is not complete enough and does not have the ability to solve the complex security related to PWA applications after using third-party browser engines. and privacy issues. Apple explained that it was not practical to work on this given the other requirements of DMA and the very low user adoption of PWAs. Therefore, in order to comply with

Use the Laravel push notification extension to implement the message push function of PHP mobile applications. The message push function of mobile applications is a very important and common requirement in current mobile application development. To achieve this functionality, we can use the push notification extension provided by the Laravel framework to simplify the development process. This article will introduce how to use the Laravel push notification extension to implement the message push function of PHP mobile applications. Install Laravel Push Notification Extension First, we need to install Laravel Push Notification
