PHP용 웹 푸시 라이브러리

· 2년 전 · 799

WebPush를 사용하면 웹 푸시 프로토콜에 설명된 대로 서버가 웹 푸시 알림을 전달하는 엔드포인트에 알림을 보낼 수 있습니다.

 

https://github.com/web-push-libs/web-push-php

 

<?php

use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;

// store the client-side `PushSubscription` object (calling `.toJSON` on it) as-is and then create a WebPush\Subscription from it
$subscription = Subscription::create(json_decode($clientSidePushSubscriptionJSON, true));

// array of notifications
$notifications = [
    [
        'subscription' => $subscription,
        'payload' => '{"message":"Hello World!"}',
    ], [
          // current PushSubscription format (browsers might change this in the future)
          'subscription' => Subscription::create([ 
              "endpoint" => "https://example.com/other/endpoint/of/another/vendor/abcdef...",
              "keys" => [
                  'p256dh' => '(stringOf88Chars)',
                  'auth' => '(stringOf24Chars)'
              ],
          ]),
          'payload' => '{"message":"Hello World!"}',
    ], [
        // old Firefox PushSubscription format
        'subscription' => Subscription::create([
            'endpoint' => 'https://updates.push.services.mozilla.com/push/abc...', // Firefox 43+,
            'publicKey' => 'BPcMbnWQL5GOYX/5LKZXT6sLmHiMsJSiEvIFvfcDvX7IZ9qqtq68onpTPEYmyxSQNiH7UD/98AUcQ12kBoxz/0s=', // base 64 encoded, should be 88 chars
            'authToken' => 'CxVX6QsVToEGEcjfYPqXQw==', // base 64 encoded, should be 24 chars
        ]),
        'payload' => 'hello !',
    ], [
        // old Chrome PushSubscription format
        'subscription' => Subscription::create([
            'endpoint' => 'https://fcm.googleapis.com/fcm/send/abcdef...',
        ]),
        'payload' => null,
    ], [
        // old PushSubscription format
        'subscription' => Subscription::create([
            'endpoint' => 'https://example.com/other/endpoint/of/another/vendor/abcdef...',
            'publicKey' => '(stringOf88Chars)',
            'authToken' => '(stringOf24Chars)',
            'contentEncoding' => 'aesgcm', // one of PushManager.supportedContentEncodings
        ]),
        'payload' => '{"message":"test"}',
    ]
];

$webPush = new WebPush();

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

/**
 * send one notification and flush directly
 * @var MessageSentReport $report
 */
$report = $webPush->sendOneNotification(
    $notifications[0]['subscription'],
    $notifications[0]['payload'] // optional (defaults null)
);

 

학원/학교/선생님 필수 문제풀이 솔루션 - 웹학교 데모

그누보드5 다국어버전 / 영카트5 다국어버전 

그누보드5 강좌 / 영카트5 강좌 / 무료 네이버 TV

By 웹학교

|
댓글을 작성하시려면 로그인이 필요합니다.

365ok IT뉴스

+
제목 글쓴이 날짜 조회
2년 전 조회 466
2년 전 조회 445
2년 전 조회 498
2년 전 조회 432
2년 전 조회 510
2년 전 조회 488
2년 전 조회 376
2년 전 조회 398
2년 전 조회 396
2년 전 조회 335
2년 전 조회 478
2년 전 조회 485
2년 전 조회 395
2년 전 조회 573
2년 전 조회 800
2년 전 조회 345
2년 전 조회 369
2년 전 조회 515
2년 전 조회 399
2년 전 조회 589
2년 전 조회 429
2년 전 조회 317
2년 전 조회 509
2년 전 조회 441
2년 전 조회 473
2년 전 조회 487
2년 전 조회 405
2년 전 조회 463
2년 전 조회 435
2년 전 조회 354