pushpad / pushpad-php
Pushpad PHP 库
v2.0.0
2022-11-30 14:18 UTC
Requires
- php: >=5.3.3
- ext-curl: *
- ext-json: *
README
Pushpad 是一种从网站和 Web 应用程序发送推送通知的服务。它使用 Push API,这是所有主流浏览器(Chrome、Firefox、Opera、Edge、Safari)都支持的标准。
即使用户不在您的网站上,通知也会实时送达,您还可以针对特定用户或发送批量通知。
安装
Composer
您可以通过 Composer 安装绑定。运行以下命令
composer require pushpad/pushpad-php
要使用绑定,请使用 Composer 的自动加载
require_once('vendor/autoload.php');
手动安装
下载此库的最新版本
$ git clone https://github.com/pushpad/pushpad-php.git
然后将此行添加到您的应用程序中
require_once('path/to/pushpad-php/init.php');
入门
首先您需要注册 Pushpad 并在那里创建一个项目。
然后设置您的认证凭据
Pushpad\Pushpad::$auth_token = '5374d7dfeffa2eb49965624ba7596a09'; Pushpad\Pushpad::$project_id = 123; # set it here or pass it as a param to methods later
auth_token
可以在用户账户设置中找到。project_id
可以在项目设置中找到。如果您的应用程序使用多个项目,您可以将project_id
作为参数传递给方法(例如$notification->deliver_to(user_id, array('project_id' => 123))
)。
收集用户对推送通知的订阅
您可以使用如入门指南所述的 JavaScript SDK 让用户订阅您的通知。
如果您需要为 uid
生成 HMAC 签名,可以使用此辅助程序
Pushpad\Pushpad::signature_for($current_user_id);
发送推送通知
$notification = new Pushpad\Notification(array( # required, the main content of the notification 'body' => "Hello world!", # optional, the title of the notification (defaults to your project name) 'title' => "Website Name", # optional, open this link on notification click (defaults to your project website) 'target_url' => "https://example.com", # optional, the icon of the notification (defaults to the project icon) 'icon_url' => "https://example.com/assets/icon.png", # optional, the small icon displayed in the status bar (defaults to the project badge) 'badge_url' => "https://example.com/assets/badge.png", # optional, an image to display in the notification content # see https://pushpad.xyz/docs/sending_images 'image_url' => "https://example.com/assets/image.png", # optional, drop the notification after this number of seconds if a device is offline 'ttl' => 604800, # optional, prevent Chrome on desktop from automatically closing the notification after a few seconds 'require_interaction' => true, # optional, enable this option if you want a mute notification without any sound 'silent' => false, # optional, enable this option only for time-sensitive alerts (e.g. incoming phone call) 'urgent' => false, # optional, a string that is passed as an argument to action button callbacks 'custom_data' => "123", # optional, add some action buttons to the notification # see https://pushpad.xyz/docs/action_buttons 'actions' => array( array( 'title' => "My Button 1", 'target_url' => "https://example.com/button-link", # optional 'icon' => "https://example.com/assets/button-icon.png", # optional 'action' => "myActionName" # optional ) ), # optional, bookmark the notification in the Pushpad dashboard (e.g. to highlight manual notifications) 'starred' => true, # optional, use this option only if you need to create scheduled notifications (max 5 days) # see https://pushpad.xyz/docs/schedule_notifications 'send_at' => strtotime('2016-07-25 10:09'), # use a function like strtotime or time that returns a Unix timestamp # optional, add the notification to custom categories for stats aggregation # see https://pushpad.xyz/docs/monitoring 'custom_metrics' => array('examples', 'another_metric') # up to 3 metrics per notification )); # deliver to a user $notification->deliver_to($user_id); # deliver to a group of users $notification->deliver_to($user_ids); # deliver to some users only if they have a given preference # e.g. only $users who have a interested in "events" will be reached $notification->deliver_to($users, ["tags" => ["events"]]); # deliver to segments # e.g. any subscriber that has the tag "segment1" OR "segment2" $notification->broadcast(["tags" => ["segment1", "segment2"]]); # you can use boolean expressions # they can include parentheses and the operators !, &&, || (from highest to lowest precedence) # https://pushpad.xyz/docs/tags $notification->broadcast(["tags" => ["zip_code:28865 && !optout:local_events || friend_of:Organizer123"]]); $notification->deliver_to($users, ["tags" => ["tag1 && tag2", "tag3"]]); # equal to "tag1 && tag2 || tag3" # deliver to everyone $notification->broadcast();
您可以在项目设置中为大多数字段设置默认值。有关通知字段的更多信息,请参阅文档。
如果您尝试向用户 ID 发送通知,但该用户未订阅,则该 ID 将被简单忽略。
上述方法返回一个数组
'id'
是 Pushpad 上通知的 ID'scheduled'
是通知的估计覆盖范围(即要发送通知的设备数量,这可能与用户数量不同,因为用户可能会在多个设备上接收通知)'uids'
(仅限deliver_to
)是实际由通知触及的用户 ID,因为这些用户已订阅您的通知。例如,如果您向['uid1', 'uid2', 'uid3']
发送通知,但只有'uid1'
已订阅,则您将收到['uid1']
作为响应。请注意,如果用户在最后发送给他的通知之后取消订阅,他可能仍然被报告为已订阅一次(这是由于 W3C Push API 的工作方式所致)。'send_at'
仅适用于计划通知。在这种情况下,不可用'scheduled'
和'uids'
字段。
许可
该库在 MIT 许可证 的条款下作为开源软件提供。