skip to Main Content

I am using "minishlink/web-push" package to send notification in my Laravel project.

Route::post("/admin/sendNotif/{sub}", function(PushSubscription $sub, Request $request){
    $webPush = new WebPush([
        "VAPID" => [
            "publicKey" => "BNbqX8M5NJJ...",
            "privateKey" => "i4I89hSrn-MGvp...",
            "subject" => "https://example.com"
            ]
        ]);
    $webPush->sendOneNotification(
        Subscription::create(json_decode($sub->data, true)),
        json_encode($request->input())
    );
    return redirect("/");
});

With this method, I can send only one notification:

    $webPush->sendOneNotification(
        Subscription::create(json_decode($sub->data, true)),
        json_encode($request->input())
    );

Is there a way I can send multiple notifications at the same time?

2

Answers


  1. Because this package uses Google service, I doubt it can do this. My suggestion is that you use Laravel echo : https://github.com/laravel/echo

    Login or Signup to reply.
  2. Have a look at this readme for a better explanation of WebPush:

    web-push-php

    Code Snippet

    // 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()}";
        }
    }

    Explanation

    • Loop through a notification array which contains the subscription object and the payload then queue the notification passing the two as arguments.
    • Then flush the queue to send the notification.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search