# WebPush
> Web Push library for PHP
[![Build Status](https://travis-ci.org/web-push-libs/web-push-php.svg?branch=master)](https://travis-ci.org/web-push-libs/web-push-php)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d60e8eea-aea1-4739-8ce0-a3c3c12c6ccf/mini.png)](https://insight.sensiolabs.com/projects/d60e8eea-aea1-4739-8ce0-a3c3c12c6ccf)
WebPush can be used to send notifications to endpoints which server delivers Web Push notifications as described in
the [Web Push protocol](https://tools.ietf.org/html/draft-thomson-webpush-protocol-00).
As it is standardized, you don't have to worry about what server type it relies on.
## Requirements
PHP 7.2+ and the following extensions:
* gmp (optional but better for performance)
* mbstring
* curl
* openssl
There is no support and maintenance for older PHP versions, however you are free to use the following compatible versions:
- PHP 5.6 or HHVM: `v1.x`
- PHP 7.0: `v2.x`
- PHP 7.1: `v3.x-v5.x`
## Installation
Use [composer](https://getcomposer.org/) to download and install the library and its dependencies.
`composer require minishlink/web-push`
## Usage
```php
<?php
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;
// array of notifications
$notifications = [
[
'subscription' => Subscription::create([
'endpoint' => 'https://updates.push.services.mozilla.com/push/abc...', // Firefox 43+,
'publicKey' => 'BPcMbnWQL5GOYX/5LKZXT6sLmHiMsJSiEvIFvfcDvX7IZ9qqtq68onpTPEYmyxSQNiH7UD/98AUcQ12kBoxz/0s=', // base 64 encoded, should be 88 chars
'authToken' => 'CxVX6QsVToEGEcjfYPqXQw==', // base 64 encoded, should be 24 chars
]),
'payload' => 'hello !',
], [
'subscription' => Subscription::create([
'endpoint' => 'https://fcm.googleapis.com/fcm/send/abcdef...', // Chrome
]),
'payload' => null,
], [
'subscription' => Subscription::create([
'endpoint' => 'https://example.com/other/endpoint/of/another/vendor/abcdef...',
'publicKey' => '(stringOf88Chars)',
'authToken' => '(stringOf24Chars)',
'contentEncoding' => 'aesgcm', // one of PushManager.supportedContentEncodings
]),
'payload' => '{msg:"test"}',
], [
'subscription' => Subscription::create([ // this is the structure for the working draft from october 2018 (https://www.w3.org/TR/2018/WD-push-api-20181026/)
"endpoint" => "https://example.com/other/endpoint/of/another/vendor/abcdef...",
"keys" => [
'p256dh' => '(stringOf88Chars)',
'auth' => '(stringOf24Chars)'
],
]),
'payload' => '{"msg":"Hello World!"}',
],
];
$webPush = new WebPush();
// send multiple notifications with payload
foreach ($notifications as $notification) {
$webPush->queueNotification(
$notification['subscription'],
$notification['payload'] // optional (defaults null)
);
}
/**
* Check sent results
* @var MessageSentReport $report
*/
foreach ($webPush->flush() as $report) {
$endpoint = $report->getRequest()->getUri()->__toString();
if ($report->isSuccess()) {
echo "[v] Message sent successfully for subscription {$endpoint}.";
} else {
echo "[x] Message failed to sent for subscription {$endpoint}: {$report->getReason()}";
}
}
/**
* send one notification and flush directly
* @var MessageSentReport $report
*/
$report = $webPush->sendOneNotification(
$notifications[0]['subscription'],
$notifications[0]['payload'] // optional (defaults null)
);
```
### Full examples of Web Push implementations
* An example with web-push-php: [Minishlink/web-push-php-example](https://github.com/Minishlink/web-push-php-example)
* Matthew Gaunt's [Web Push Book](https://web-push-book.gauntface.com) - a must read
* Mozilla's [ServiceWorker Cookbooks](https://serviceworke.rs/push-payload_index_doc.html) (don't mind the `server.js` file: it should be replaced by your PHP server code with this library)
* Google's [introduction to push notifications](https://developers.google.com/web/fundamentals/getting-started/push-notifications/) (as of 03-20-2016, it doesn't mention notifications with payload)
* you may want to take a look at my own implementation: [sw.js](https://github.com/Minishlink/physbook/blob/9bfcc2bbf7311a5de4628eb8f3ae56b6c3e74067/web/service-worker.js) and [app.js](https://github.com/Minishlink/physbook/blob/02a0d5d7ca0d5d2cc6d308a3a9b81244c63b3f14/app/Resources/public/js/app.js)
### Authentication (VAPID)
Browsers need to verify your identity. A standard called VAPID can authenticate you for all browsers. You'll need to create and provide a public and private key for your server. These keys must be safely stored and should not change.
You can specify your authentication details when instantiating WebPush. The keys can be passed directly (recommended), or you can load a PEM file or its content:
```php
<?php
use Minishlink\WebPush\WebPush;
$endpoint = 'https://fcm.googleapis.com/fcm/send/abcdef...'; // Chrome
$auth = [
'VAPID' => [
'subject' => 'mailto:[email protected]', // can be a mailto: or your website address
'publicKey' => '~88 chars', // (recommended) uncompressed public key P-256 encoded in Base64-URL
'privateKey' => '~44 chars', // (recommended) in fact the secret multiplier of the private key encoded in Base64-URL
'pemFile' => 'path/to/pem', // if you have a PEM file and can link to it on your filesystem
'pem' => 'pemFileContent', // if you have a PEM file and want to hardcode its content
],
];
$webPush = new WebPush($auth);
$webPush->queueNotification(...);
```
In order to generate the uncompressed public and secret key, encoded in Base64, enter the following in your Linux bash:
```
$ openssl ecparam -genkey -name prime256v1 -out private_key.pem
$ openssl ec -in private_key.pem -pubout -outform DER|tail -c 65|base64|tr -d '=' |tr '/+' '_-' >> public_key.txt
$ openssl ec -in private_key.pem -outform DER|tail -c +8|head -c 32|base64|tr -d '=' |tr '/+' '_-' >> private_key.txt
```
If you can't access a Linux bash, you can print the output of the `createVapidKeys` function:
```php
var_dump(VAPID::createVapidKeys()); // store the keys afterwards
```
On the client-side, don't forget to subscribe with the VAPID public key as the `applicationServerKey`: (`urlBase64ToUint8Array` source [here](https://github.com/Minishlink/physbook/blob/02a0d5d7ca0d5d2cc6d308a3a9b81244c63b3f14/app/Resources/public/js/app.js#L177))
```js
serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
})
```
#### Reusing VAPID headers
VAPID headers make use of a JSON Web Token (JWT) to verify your identity. That token payload includes the protocol and hostname of the endpoint included in the subscription and an expiration timestamp (usually between 12-24h), and it's signed using your public and private key. Given that, two notifications sent to the same push service will use the same token, so you can reuse them for the same flush session to boost performance using:
```php
$webPush->setReuseVAPIDHeaders(true);
```
### Notifications and default options
Each notification can have a specific Time To Live, urgency, and topic.
You can change the default options with `setDefaultOptions()` or in the constructor:
```php
<?php
use Minishlink\WebPush\WebPush;
$defaultOptions = [
'TTL' => 300, // defaults to 4 weeks
'urgency' => 'normal', // protocol defaults to "normal"
'topic' => 'new_event', // not defined by default,
'batchSize' => 200, // defaults to 1000
];
// for every notifications
$webPush = new WebPush([], $defaultOptions);
$webPush->setDefaultOptions($defaultOptions);
// or for one notification
$webPush->sendOneNotification($subscription, $payload, ['TTL' => 5000]);
```
#### TTL
没有合适的资源?快使用搜索试试~ 我知道了~
Invision Community 论坛源码
共2000个文件
php:3122个
json:2092个
html:1121个
需积分: 15 0 下载量 190 浏览量
2022-03-14
01:46:25
上传
评论
收藏 18.87MB ZIP 举报
温馨提示
Invision Community 论坛源码
资源详情
资源评论
资源推荐
收起资源包目录
Invision Community 论坛源码 (2000个子文件)
bootstrap.min.css 95KB
editor_ie7.css 34KB
editor_iequirks.css 33KB
editor_ie8.css 33KB
editor_ie.css 32KB
editor_gecko.css 32KB
editor.css 31KB
editor_ie7.css 31KB
editor_iequirks.css 30KB
editor_ie8.css 30KB
editor_ie.css 29KB
editor_gecko.css 29KB
editor.css 28KB
fonts.css 21KB
fonts.css 21KB
jquery-ui.min.css 16KB
dialog_ie7.css 15KB
bootstrap-theme.min.css 15KB
dialog_ie7.css 15KB
dialog_ie8.css 14KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog_ie8.css 14KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog.css 13KB
dialog.css 13KB
codemirror.css 12KB
forms.css 10KB
forms.css 10KB
spectrum.css 10KB
jquery.ui.plupload.css 6KB
misc.css 4KB
jquery.plupload.queue.css 4KB
misc.css 4KB
cropper.min.css 4KB
buttons.css 4KB
global.css 4KB
global.css 3KB
contents.css 3KB
buttons.css 3KB
layout.css 2KB
layout.css 2KB
reset.css 2KB
reset.css 2KB
jumbotron.css 1KB
installer.css 1KB
typography.css 958B
typography.css 958B
installer.css 902B
messages.css 898B
messages.css 897B
colordialog.css 792B
ConfigForm.css 311B
dialog.css 249B
readme.html 3KB
upgrading.html 2KB
installing.html 1KB
index.html 217B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
index.html 0B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
laoliu1996
- 粉丝: 46
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于python开发使用深度学习去预测股票后续的价格+源码+文档(毕业设计&课程设计&项目开发)
- flowable-designer-5.22.0.zip
- threadmanager.cpp
- 腾讯云小程序 - 一站式开发与部署平台
- 基于JSP+Java+Servlet采用MVC模式开发的购物网站+源码(毕业设计&课程设计&项目开发)
- fastgestures安装包,模拟mac的触控板收拾,两指代表右击, 三指拖拽
- 基于组态王的升降式横移立体车库控制系统+源码(毕业设计&课程设计&项目开发)
- 基于python+Django和协同过滤算法的电影推荐系统+源码(毕业设计&课程设计&项目开发)
- 环境配置 vscode+jupyter
- 项目全部代码,还包含使用到的图片
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0