alexlisenkov/laravel-web-push 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alexlisenkov/laravel-web-push

最新稳定版本:1.1.0

Composer 安装命令:

composer require alexlisenkov/laravel-web-push

包简介

Send push notifications from your backend via the Web Push Protocol

README 文档

README

Total Downloads Coverage Status CI

More info

The alexlisenkov/laravel-web-push package is a package to send push notifications. Send out push messages as a standalone package. Use this if you dont work with laravel notification channels.

If you are new to the Web Push Protocol please read about the fundamentals.

Installation

composer require alexlisenkov/laravel-web-push
php artisan vendor:publish --provider="AlexLisenkov\LaravelWebPush\LaravelWebPushServiceProvider"

Configuration

To send out Web Push notifications you need to generate yourself an identity. The simplest thing to do is to visit https://web-push-codelab.glitch.me

Open up config/laravel-web-push.php

Copy the public key and private key into your configuration. Please note that this public key is the same as you will use in the applicationServerKey in the JavaScript pushManager api.

<?php
/*
 * To generate a application server keys
 * Visit: https://web-push-codelab.glitch.me/
 */

return [
    'public_key' => '',
    'private_key' => '',
    'subject' => config('APP_URL', 'mailto:me@website.com'),
    'expiration' => 43200,
    'TTL' => 2419200,
];

Sending a Web Push

Quick guide

A message can be created by creating a new AlexLisenkov\LaravelWebPush\PushMessage class.

Please see this MDN doc or the Living Standards to see all available options.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use AlexLisenkov\LaravelWebPush\PushMessage;
use AlexLisenkov\LaravelWebPush\PushSubscription;

class PushMessageController
{
    public function sendPushMessage(): Response
    {
        // Create a subscription
        $subscription = new PushSubscription(
            "endpoint",
            "p256dh",
            "auth"
        );
        
        // Create a message
        $message = new PushMessage();
        $message->setTitle('Hello World');
        $message->setBody('This message is sent using web push');
        $message->setIcon('https://placekitten.com/75/75');
        
        // We can either use the message to send it to a subscription
        $message->sendTo($subscription)->wait();
        
        // Or send the subscription a message
        $subscription->sendMessage($message)->wait();
        
        return response('ok');
    }
}

Creating message objects

A message can be created by creating a new class that extends the AlexLisenkov\LaravelWebPush\PushMessage class. Please see this MDN doc or the Living Standards to see all available options.

<?php

namespace App\Http\Controllers;

use AlexLisenkov\LaravelWebPush\PushMessage;

class ExampleMessage extends PushMessage
{
    protected $title = 'Hello world';

    protected $body = 'This message is sent using web push';

    protected $icon = 'https://placekitten.com/75/75';
    
    // Or override a getter
    public function getData()
    {
        return User()->name;
    }
}

Creating a subscription

The AlexLisenkov\LaravelWebPush\PushSubscription is used to create a new subscription.

<?php
use AlexLisenkov\LaravelWebPush\PushSubscription;

new PushSubscription(
        "endpoint",
        "p256dh",
        "auth"
    );

It is also possible for you to implement AlexLisenlov\LaravelWebPush\Contracts\PushSubscriptionContract into any class. For example on a model.

Sending a notification

Now that we have a subscriber and a message, we can send it out.

<?php
use AlexLisenkov\LaravelWebPush\PushSubscription;

// Create a new message
$message = new ExampleMessage();

// Create a new subscription
$subscription = new PushSubscription(
        "endpoint",
        "p256dh",
        "auth"
    );

// We can either use the message to send it to a subscription
$message->sendTo($subscription);

// Or send the subscription a message
$subscription->sendMessage($message);

Service worker

Show a notification to the subscriber by adding an event listener in your service worker.

self.addEventListener('push', function(e) {
    let data = e.data.json();
    
    self.registration.showNotification(data.title, data.options);
});

Testing

$ composer test

Security

If you discover any security related issues, please email alex@create.nl instead of using the issue tracker.

Contributing

Contributions are welcome.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 4.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-24