pushpad/pushpad-php
Composer 安装命令:
composer require pushpad/pushpad-php
包简介
Pushpad PHP Library
README 文档
README
Pushpad is a service for sending push notifications from websites and web apps. It uses the Push API, which is supported by all major browsers (Chrome, Firefox, Opera, Edge, Safari).
Notifications are delivered in real time even when the users are not on your website and you can target specific users or send bulk notifications.
Installation
Composer
This package requires PHP 8.0+ with the curl extension.
Install the SDK via Composer:
composer require pushpad/pushpad-php
Then include Composer's autoloader:
require_once __DIR__ . '/vendor/autoload.php';
Manual installation
Clone the repository and require the bootstrap file in your project:
git clone https://github.com/pushpad/pushpad-php.git
require_once __DIR__ . '/path/to/pushpad-php/init.php';
Getting started
First sign up to Pushpad and create a project.
Configure the SDK with your credentials before you make any API calls:
Pushpad\Pushpad::$authToken = '5374d7dfeffa2eb49965624ba7596a09'; Pushpad\Pushpad::$projectId = 123; // set a default project (optional)
authTokencan be created in the account settings.projectIdis shown in the project settings. If you work with multiple projects you can pass a different project id to individual method calls instead of configuring the global default.
Collecting user subscriptions
Use the JavaScript SDK to subscribe users to push notifications (see the getting started guide).
When you need to sign a uid, generate the HMAC signature with:
$signature = Pushpad\Pushpad::signatureFor((string) $currentUserId);
Sending push notifications
Use Pushpad\Notification::create() (or the send() alias) to create and send a notification:
$response = Pushpad\Notification::create([ // required content 'body' => 'Hello world!', // optional fields 'title' => 'Website Name', 'target_url' => 'https://example.com', 'icon_url' => 'https://example.com/assets/icon.png', 'badge_url' => 'https://example.com/assets/badge.png', 'image_url' => 'https://example.com/assets/image.png', 'ttl' => 604800, 'require_interaction' => true, 'silent' => false, 'urgent' => false, 'custom_data' => '123', 'actions' => [ [ 'title' => 'My Button 1', 'target_url' => 'https://example.com/button-link', 'icon' => 'https://example.com/assets/button-icon.png', 'action' => 'myActionName', ], ], 'starred' => true, 'send_at' => (new DateTimeImmutable('+1 hour'))->format(DATE_ATOM), 'custom_metrics' => ['examples', 'another_metric'], // targeting options 'uids' => ['user-1', 'user-2'], 'tags' => ['segment1', 'segment2'], ]);
- Omit
uidsandtagsto broadcast to everyone. - If you set
uidsand some users are not subscribed to notifications, Pushpad ignores them. - Use boolean expressions inside
tagsfor complex segments (e.g.'zip_code:28865 && !optout:local_events'). - Scheduled notifications require an ISO 8601 timestamp in
send_at(as produced byDateTimeInterface::format(DATE_ATOM)). - You can set default values for most notification fields in the project settings.
- Refer to the REST API docs for more details about the notification fields and their usage.
The response includes useful information:
// Notification ID $notificationId = $response['id']; // Estimated number of devices that will receive the notification // Not available for notifications that use send_at $estimatedReach = $response['scheduled']; // Available only if you specify some user IDs (uids) in the request: // it indicates which of those users are subscribed to notifications. // Not available for notifications that use send_at $reachedUids = $response['uids']; // The time when the notification will be sent. // Available for notifications that use send_at $scheduledAt = $response['send_at'];
Getting push notification data
Fetch a single notification and inspect its attributes:
$notification = Pushpad\Notification::find(42); echo $notification->title; // "Foo Bar" echo $notification->target_url; // "https://example.com" echo $notification->ttl; // 604800 echo $notification->created_at; // ISO 8601 string echo $notification->successfully_sent_count; // 4 echo $notification->opened_count; // 2 // ... and many other attributes print_r($notification->toArray());
List notifications for a project (pagination supported through the page query parameter):
$notifications = Pushpad\Notification::findAll(['page' => 1]); foreach ($notifications as $item) { printf("Notification %d: %s\n", $item->id, $item->title); }
Pass the project id as the second argument when you prefer not to rely on the globally configured Pushpad\Pushpad::$projectId:
Pushpad\Notification::findAll([], projectId: 456);
If you need to refresh a previously loaded notification, call $notification->refresh() to hydrate the latest data from the API.
Scheduled notifications
Create a notification that will be sent later:
Pushpad\Notification::create([ 'body' => 'This notification will be sent after 60 seconds', 'send_at' => (new DateTimeImmutable('+60 seconds'))->format(DATE_ATOM), ]);
Cancel a scheduled notification when it is still pending:
$notification = Pushpad\Notification::find(5); $notification->cancel();
Getting subscription count
Retrieve the number of subscriptions associated with a project, optionally filtered by user IDs or tags:
$total = Pushpad\Subscription::count(); $byUser = Pushpad\Subscription::count(['uids' => ['user1']]); $byTags = Pushpad\Subscription::count(['tags' => ['sports && travel']]); $combined = Pushpad\Subscription::count(['uids' => ['user1'], 'tags' => ['sports && travel']], 5);
The second argument lets you override the project id if you did not configure Pushpad\Pushpad::$projectId or you need to switch project on the fly.
Getting push subscription data
Fetch subscriptions with optional filters and pagination:
$subscriptions = Pushpad\Subscription::findAll(['tags' => ['sports'], 'page' => 2]); foreach ($subscriptions as $subscription) { echo $subscription->id . PHP_EOL; // ... }
Load a specific subscription when you already know its id:
$subscription = Pushpad\Subscription::find(123); echo $subscription->id; echo $subscription->project_id; echo $subscription->endpoint; echo $subscription->uid; echo $subscription->tags; echo $subscription->last_click_at; echo $subscription->created_at; // ... and many other attributes print_r($subscription->toArray());
Updating push subscription data
Although tags and user IDs are usually managed from the JavaScript SDK, you can also update them from server:
$subscriptions = Pushpad\Subscription::findAll(['uids' => ['user1']]); foreach ($subscriptions as $subscription) { $tags = $subscription->tags ?? []; $tags[] = 'another_tag'; $subscription->update([ 'uid' => 'myuser1', 'tags' => array_values(array_unique($tags)), ]); }
Importing push subscriptions
To import existing subscriptions or seed test data use Pushpad\Subscription::create():
$subscription = Pushpad\Subscription::create([ 'endpoint' => 'https://example.com/push/f7Q1Eyf7EyfAb1', 'p256dh' => 'BCQVDTlYWdl05lal3lG5SKr3VxTrEWpZErbkxWrzknHrIKFwihDoZpc_2sH6Sh08h-CacUYI-H8gW4jH-uMYZQ4=', 'auth' => 'cdKMlhgVeSPzCXZ3V7FtgQ==', 'uid' => 'exampleUid', 'tags' => ['exampleTag1', 'exampleTag2'], ]);
Typically subscriptions are collected from the browser using the JavaScript SDK; server-side creation should be reserved for migrations and special workflows.
Deleting push subscriptions
Delete subscriptions programmatically (use with care, the operation is irreversible):
$subscription = Pushpad\Subscription::find(123); $subscription->delete();
Managing projects
Projects can also be managed via the API for automation use cases:
$project = Pushpad\Project::create([ 'sender_id' => 123, 'name' => 'My project', 'website' => 'https://example.com', 'icon_url' => 'https://example.com/icon.png', 'badge_url' => 'https://example.com/badge.png', 'notifications_ttl' => 604800, 'notifications_require_interaction' => false, 'notifications_silent' => false, ]); $projects = Pushpad\Project::findAll(); $project = Pushpad\Project::find(123); $project->update(['name' => 'The New Project Name']); $project->delete();
Managing senders
Senders hold the VAPID credentials used for Web Push:
$sender = Pushpad\Sender::create([ 'name' => 'My sender', // omit the keys below to let Pushpad generate them automatically // 'vapid_private_key' => '-----BEGIN EC PRIVATE KEY----- ...', // 'vapid_public_key' => '-----BEGIN PUBLIC KEY----- ...', ]); $senders = Pushpad\Sender::findAll(); $sender = Pushpad\Sender::find(987); $sender->update(['name' => 'The New Sender Name']); $sender->delete();
License
The library is available as open source under the terms of the MIT License.
pushpad/pushpad-php 适用场景与选型建议
pushpad/pushpad-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147.99k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2016 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「notifications」 「push notifications」 「pushpad」 「Web Push」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pushpad/pushpad-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pushpad/pushpad-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pushpad/pushpad-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova package that adds a notification feed in your Nova app.
Apple Push Notification & Feedback Provider
A PSR-7 compatible library for making CRUD API endpoints
Laravel package to send push notifications to mobile devices (apns, gcm)
Captures outgoing SMS notifications
Send push notification to android|ios devices , support APNs, FCM
统计信息
- 总下载量: 147.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 26
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-12