laravel 如何使用“minishlink/web-push”包发送多个通知?

0x6upsns  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(71)

我在Laravel项目中使用***“minishlink/web-push”***包发送通知。

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

字符串
使用此方法,我只能发送一个通知:

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


有没有一种方法可以同时发送多个通知?

brtdzjyr

brtdzjyr1#

看看这个自述文件,以更好地解释WebPush:
web-push-php

代码片段

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

字符串

说明

  • 循环遍历包含订阅对象和有效负载的通知数组,然后将通知排队,并将这两个对象作为参数传递。
  • 然后刷新队列以发送通知。
dxxyhpgq

dxxyhpgq2#

因为这个包使用谷歌服务,我怀疑它能做到这一点。我的建议是使用 *Laravel echo *:https://github.com/laravel/echo

相关问题