以每小时 20 美元的价格聘请具备人工智能专业知识的 Laravel 开发人员。48 小时内即可开始工作。

Laravel Web Push Notifications

最后更新于 经过

Laravel Web Push Notifications image

Web push notifications let your app reach users even when they don't have a browser tab open — no native app required. The Laravel Notification Channels project is a community collection of notification drivers for Laravel, and their Web Push Notifications Channel adds push notifications into Laravel's existing notification system, so you write a notification class the same way you would for mail or Slack. Under the hood, it talks directly to the browser's Push API using VAPID keys, with no third-party push service in between. Chrome, Firefox, Edge, and Safari are all supported.

安装

Pull in the package via Composer, then add the HasPushSubscriptions 你的特质 User model — this is what gives users their push subscription methods:

作曲家 要求 laravel-通知通道/webpush
使用 NotificationChannels\WebPush\HasPushSubscriptions ;
班级 用户 延伸 模型
{
使用 HasPushSubscriptions ;
}

The package needs a database table to store subscriptions, so publish and run its migration:

php 工匠 供应商:发布 --提供者= "NotificationChannels\WebPush\WebPushServiceProvider" --标签= "migrations"
php 工匠 迁移

Publish the config file if you need to customise anything:

php 工匠 供应商:发布 --提供者= "NotificationChannels\WebPush\WebPushServiceProvider" --标签= “配置”

Finally, generate a VAPID key pair:

php 工匠 webpush:vapid

This adds VAPID_PUBLIC_KEYVAPID_PRIVATE_KEY 你的 .env file. The public key gets shared with the browser when a user subscribes; the private key signs outgoing push messages so browsers can verify they came from you. Treat these like any other secret, and don't rotate them — every existing subscription is tied to the key pair that created it.

If you're targeting Safari or iOS, you'll also need to add a VAPID_SUBJECT — a URL or mailto: address that identifies your app. Apple requires it and will return a BadJwtToken error without it.

客户端设置

Web push requires two things in the browser: a service worker to receive and display notifications, and a call to PushManager.subscribe() to get the subscription object you'll save on the server.

笔记: The Push API only works in a secure context — your site must be served over HTTPS. http://localhost is the one exception and works without TLS for local development.

Service Worker

创建一个 public/sw.js file. The service worker runs in the background and handles incoming push events:

self. 添加事件监听器 'push' , 功能 事件 ){
数据 = {};
尝试 {
数据 = event.data?. json () ?? {};
} 抓住 (e) {
数据 = { title: 'Notification' , body: event.data?. 文本 () ?? “” };
}
事件。 waitUntil
self.registration. 显示通知 (data.title, {
body: data.body,
icon: data.icon,
badge: data.badge,
data: data.data,
actions: data.actions,
})
(英文):
});
self. 添加事件监听器 'notificationclick' , 功能 事件 ){
event.notification. 关闭 ();
常量 网址 = event.notification.data?.url ?? ‘/’ ;
事件。 waitUntil
self.clients. matchAll ({ type: 'window' , includeUncontrolled: 真的 })。 然后 功能 clientList ){
为了 常量 客户 clientList) {
如果 (client.url === 网址 && 'focus' client) {
返回 client. focus ();
}
}
返回 self.clients. openWindow (url);
})
(英文):
});

notificationclick handler checks whether a window is already open at the target URL and focuses it rather than opening a duplicate tab.

Subscribing

The VAPID public key needs to be available to your JavaScript. A simple way is a meta tag in your layout:

< 姓名 = "vapid-public-key" 内容 = "{{ config('webpush.vapid.public_key') }}" >

Wire this up to a button click — browsers block the permission prompt unless it's triggered by a user gesture:

// VAPID keys are Base64URL-encoded; atob() requires standard Base64, so we convert first
功能 urlBase64ToUint8Array base64String ){
常量 padding = '=' 重复 (( 4 - (base64String. 长度 4 )) 4 (英文):
常量 base64 = (base64String + padding). 代替 /-/ , ‘+’ )。 代替 /_/ , ‘/’ (英文):
常量 rawData = atob (base64);
返回 Uint8Array ([ ... rawData]. 地图 (( c => c. charCodeAt 0 )));
}
异步 功能 订阅 (){
如果 'serviceWorker' navigator) || 'PushManager' window)) {
返回 ;
}
常量 允许 = 等待 Notification. requestPermission ();
如果 (permission !== 'granted' ){
返回 ;
}
等待 navigator.serviceWorker. 登记 '/sw.js' (英文):
常量 登记 = 等待 navigator.serviceWorker.ready;
常量 contentEncoding = (PushManager.supportedContentEncodings || [ 'aesgcm' ])[ 0 ];
常量 订阅 = 等待 registration.pushManager. 订阅 ({
userVisibleOnly: 真的 ,
applicationServerKey: urlBase64ToUint8Array
文档。 查询选择器 'meta[name="vapid-public-key"]' ).content
),
});
常量 { 端点 , :{ p256dh , 授權 } } = subscription. toJSON ();
等待 拿来 '/push/subscribe' ,{
方法: '邮政' ,
标题:{
‘内容类型’ : ‘应用程序/json’ ,
'X-CSRF-TOKEN' : document. 查询选择器 'meta[name="csrf-token"]' ).content,
},
body: JSON 字符串化 ({ endpoint, key: p256dh, token: auth, encoding: contentEncoding }),
});
}

navigator.serviceWorker.ready is used rather than the registration returned by register() 因为 register() resolves as soon as the script is parsed, not when the worker is active. ready waits for an active worker before calling pushManager.subscribe()

/push/subscribe route on your server receives those four values and passes them to updatePushSubscription() on the authenticated user, covered in the next section.

Managing Subscriptions

On the server side, store what the browser just sent by calling updatePushSubscription() on the user:

$用户 -> updatePushSubscription ($endpoint, $key, $token, $contentEncoding);

You can also clean up when a user opts out:

$用户 -> deletePushSubscription ($endpoint);

One thing you don't have to manage manually is expired subscriptions. When a delivery attempt comes back with an expired endpoint, the package detects it and removes the subscription from the database automatically.

Sending a Notification

Once subscriptions are in place, sending a push notification is the same as any other Laravel notification. Add WebPushChannelvia() method and implement toWebPush() :

使用 照亮\通知\通知 ;
使用 NotificationChannels\WebPush\WebPushChannel ;
使用 NotificationChannels\WebPush\WebPushMessage ;
班级 GoalScored 延伸 通知
{
民众 功能 __构造
受保护 细绳 $scorer,
受保护 细绳 团队,
受保护 整数 $minute,
){}
民众 功能 通过 (需通知的) : 大批
{
返回 [ WebPushChannel ::班级 ];
}
民众 功能 toWebPush ($notifiable, $notification) : WebPushMessage
{
返回 新的 WebPushMessage
-> 标题 'GOAL! ' $this -> team)
-> 图标 '/icons/football.png'
-> 身体 "{ $this -> scorer } scores in the { $this -> 分钟 }' ⚽"
-> 行动 'View match' , 'view_match'
-> 数据 ([ 'url' => '/matches/live' ])
-> 选项 ([ 'TTL' => 60 ]);
}
}

A short TTL makes sense for a goal alert — a notification that arrives an hour later isn't useful. WebPushMessage covers the full Push API surface: alongside title , body , icon , 和 action ,你可以设置 badge , image , tag , vibrate , requireInteraction , 和更多。

To send it, call notify() on the user as you would with any other Laravel notification:

$用户 -> 通知 新的 GoalScored scorer : 埃里克·巴恩斯 , 团队 : 'Laravel News FC' , 分钟 : 29 ));

Declarative Web Push

A newer alternative is DeclarativeWebPushMessage , which targets the Declarative Web Push spec. The main motivation is that traditional web push requires a service worker to be running to handle the push event — declarative push moves that logic to the browser itself, which has privacy and battery-life benefits on mobile. Browser support is still limited, but the API mirrors WebPushMessage closely:

使用 NotificationChannels\WebPush\DeclarativeWebPushMessage ;
使用 NotificationChannels\WebPush\WebPushChannel ;
民众 功能 toWebPush ($notifiable, $notification) : DeclarativeWebPushMessage
{
返回 新的 DeclarativeWebPushMessage
-> 标题 'GOAL! ' $this -> team)
-> 图标 '/icons/football.png'
-> 身体 "{ $this -> scorer } scores in the { $this -> 分钟 }' ⚽"
-> 行动 'View match' , 'view_match' , 'https://myapp.com/matches/live'
-> navigate 'https://myapp.com/matches/live' (英文):
}

The key difference is navigate() , which tells the browser where to send the user when they tap the notification — no service worker notificationclick handler needed. Actions also accept a URL as a third argument for the same reason.

You can view the source code and learn more about the Web Push Notifications channel on GitHub

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

访问 Laravel Cloud
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
Laravel Schema Sentinel: Detect and Fix Database Schema Drift image

Laravel Schema Sentinel: Detect and Fix Database Schema Drift

阅读文章
RedBerry to Host Georgia's First Laravel Meetup in Tbilisi image

RedBerry to Host Georgia's First Laravel Meetup in Tbilisi

阅读文章
Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

阅读文章
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

阅读文章
Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

阅读文章
Polyscope for Windows is Now Available image

Polyscope for Windows is Now Available

阅读文章