All Projects → AlexLisenkov → laravel-web-push

AlexLisenkov / laravel-web-push

Licence: MIT license
Laravel package for sending out push notifications

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-web-push

OneSignal-Codeigniter-Push-Notification
OneSignal is a free push notification service for web and mobile apps. This Codeigniter example makes it easy to integrate your website with OneSignal Push Notifications. https://onesignal.com/ DEMO - http://ci3onesignal.codefort.ru/
Stars: ✭ 27 (+92.86%)
Mutual labels:  push-notifications, web-push
browser-push
Complete workout and guidelines to add web push notifications support for your webapp without third-party notification provider
Stars: ✭ 67 (+378.57%)
Mutual labels:  push-notifications, web-push
WaiterBell
WaiterBell - The ticketing system in real world!
Stars: ✭ 16 (+14.29%)
Mutual labels:  push-notifications
app-version-laravel
Laravel application versioning
Stars: ✭ 24 (+71.43%)
Mutual labels:  package
local-reminders
Local Scheduled Push Notification using Firebase
Stars: ✭ 26 (+85.71%)
Mutual labels:  push-notifications
nativescript-pushy
Easy push notifications for your NativeScript app!
Stars: ✭ 19 (+35.71%)
Mutual labels:  push-notifications
tehybug
Low Power WIFI environmental data trackers based on ESP8266 Module
Stars: ✭ 15 (+7.14%)
Mutual labels:  push-notifications
hms-cordova-plugin
This repo contains all of Cordova HMS plugins.
Stars: ✭ 78 (+457.14%)
Mutual labels:  push-notifications
cft
Climate futures toolbox: easy MACA (MACAv2) climate data access 📦
Stars: ✭ 16 (+14.29%)
Mutual labels:  package
dataset
qri dataset definition
Stars: ✭ 16 (+14.29%)
Mutual labels:  package
gotify-push
Chrome Extension for Send Push Notification 🔔 to gotify/server ☁
Stars: ✭ 32 (+128.57%)
Mutual labels:  push-notifications
mobile-messaging-cordova-plugin
Mobile Messaging SDK plugin for Cordova projects
Stars: ✭ 19 (+35.71%)
Mutual labels:  push-notifications
mobile-messaging-sdk-ios
Mobile Messaging SDK for iOS
Stars: ✭ 45 (+221.43%)
Mutual labels:  push-notifications
notifire
The open-source notification infrastructure for developers
Stars: ✭ 12,436 (+88728.57%)
Mutual labels:  push-notifications
laration
Simple package to see all current configurations being used by your Laravel application
Stars: ✭ 47 (+235.71%)
Mutual labels:  package
apprise-ga
GitHub Action to send a dynamic push notification to every single platform thanks to the Apprise library
Stars: ✭ 18 (+28.57%)
Mutual labels:  push-notifications
Batch-iOS-SDK
Batch SDK for iOS
Stars: ✭ 28 (+100%)
Mutual labels:  push-notifications
node-apn
Apple Push Notification module for Node.js
Stars: ✭ 128 (+814.29%)
Mutual labels:  push-notifications
publish
Publish your module with one command in Deno.
Stars: ✭ 16 (+14.29%)
Mutual labels:  package
klee
A personnal UI library made as an excuse to have a published UI package
Stars: ✭ 19 (+35.71%)
Mutual labels:  package

Send Push Notification in Laravel

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:[email protected]'),
    '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 [email protected] instead of using the issue tracker.

Contributing

Contributions are welcome.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].