awelty/web-push-php

此包的最新版本(dev-master)没有可用的许可证信息。

使用PHP发送web推送

dev-master 2018-07-15 17:46 UTC

This package is not auto-updated.

Last update: 2024-09-21 15:21:29 UTC


README

PHP的Web推送库。

这是对@Minishlink 的Web推送库的巨大重构。

安装

composer require awelty/web-push-php

创建推送管理器

使用Silex...

我们提供了一个Silex ServiceProvider

<?php 

use Awelty\Component\WebPush\Silex\PushServiceProvider;

$app->register(new PushServiceProvider(), [
    
    'vapid.public_key' => 'publicKey...',
    'vapid.private_key' => 'privateKey...',
    'vapid.subject' => 'subject...',
    'push.dispatcher' => 'dispatcher' // false to disable events feature, otherwise the name of an EventDispatcherService
]);

这将定义一个 push.manager 服务,我们将使用它发送推送。

或手动

这将会更详细一些

<?php 

use Awelty\Component\WebPush\Model\VAPID;
use Awelty\Component\WebPush\PayloadEncrypter;
use Awelty\Component\WebPush\PushManager;
use Awelty\Component\WebPush\VapidHeadersProvider;
use Base64Url\Base64Url;

// create the VapidHeadersProvider
$vapid = new VAPID($subject, Base64Url::decode($publicKey), Base64Url::decode($privateKey));
$vapidHeadersProvider = new VapidHeadersProvider($vapid);

// create the PushManager
$pushManager = new PushManager($vapidHeadersProvider, new PayloadEncrypter(), $defaultOptions = []);

// optionnal : enable events feature (link vers doc chapter)
$pushManager->setEventDispatcher($eventDispatcher);

用法

<?php 

// Get a subscription, for exemple from your database.. 
// This is a model of the subscription you get from the front
$subscription = new Subscription($userSubscription->endpoint, $userSubscription->keys->auth, $userSubscription->keys->p256dh);

// push as a "ping"
$pushManager->push($subscription);

// push with some payload, can be a string, an object, an array.. (non scalar values will be json_encoded)
$pushManager->push($subscription, 'test');
$pushManager->push($subscription, ['title' => 'This is a push']);

// and you can provide some other options than default one for each notifications
$pushManager->push($subscription, 'test', ['TTL' => 86400]);

选项

TTL
紧急程度
主题