Php:创建网络推送通知 [英] Php: Create web push notification

查看:111
本文介绍了Php:创建网络推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PHP 中创建一个网络推送通知,但我没有它的确切程序.以下是我找到的代码,但它不起作用.

I want to create a web push notification in Php but I don't have the exact procedure for it. Following is the code I found, but it is not working.

  <?php
   require __DIR__ . '/../vendor/autoload.php';    
   use Minishlink\WebPush\WebPush;



    $subscription = json_decode(file_get_contents('php://input'), true);

    $auth = array(
   'VAPID' => array(
    'subject' => '`enter code here`',
    'publicKey' => '**********',
    'privateKey' => '***********',
   ),
 );

    $webPush = new WebPush($auth);

    $res = $webPush->sendNotification(
    $subscription['endpoint'],
    "Hello!",
     $subscription['key'],
     $subscription['token'],
     true
   );

请建议正确的步骤.

推荐答案

我花了一些时间来解决这个问题.我正在发布代码,因为它对我有用.从这里生成密钥 https://web-push-codelab.glitch.me/

I spent some time my self figuring this out. I'm posting the code as it works for me. Generate the keys from here https://web-push-codelab.glitch.me/

<?php

require_once './vendor/autoload.php';

use Minishlink\WebPush\WebPush;


// array of notifications
$notifications = array(
 array(
        'endpoint' => 'https://fcm.googleapis.com/fcm/send/abcd........', // Chrome
        'payload' => 'Hello',
        'userPublicKey' => 'BFHh..........',
        'userAuthToken' => 'DI............',
    )
);

$auth = array(
    'GCM' => 'AAAAKTK8bp4..............', // deprecated and optional, it's here only for compatibility reasons
    'VAPID' => array(
        'subject' => 'Some Text', // can be a mailto: or your website address
        'publicKey' => 'BGsm2vrV2AMpT.............', // (recommended) uncompressed public key P-256 encoded in Base64-URL
        'privateKey' => 'a89H............', // (recommended) in fact the secret multiplier of the private key encoded in Base64-URL

    ),
);

$defaultOptions = array(
    'TTL' => 300, // defaults to 4 weeks
    'urgency' => 'normal', // protocol defaults to "normal"
    'topic' => 'push', // not defined by default - collapse_key
);

$webPush = new WebPush($auth, $defaultOptions);

$vr = $webPush->sendNotification(
    $notifications[0]['endpoint'],
    $notifications[0]['payload'], // optional (defaults null)
    $notifications[0]['userPublicKey'], // optional (defaults null)
    $notifications[0]['userAuthToken'], // optional (defaults null)
    true // optional (defaults false)
);

这篇关于Php:创建网络推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆