【问题标题】:Push notifications in PHP. How to use it?在 PHP 中推送通知。如何使用它?
【发布时间】:2017-06-08 15:12:02
【问题描述】:

我想用 PHP 发送推送通知,但我不知道该怎么做。

我成功创建了 Service Worker。

我从这个页面https://web-push-codelab.appspot.com/拿了一把钥匙。但我不明白他们为什么要提供私钥?

然后我测试我的推送通知,并从这个页面发送一些通知。 但我想从我的 PHP 请求中发送推送通知。 更多,我会在所有可能的浏览器上工作,这个代码只适用于谷歌浏览器。我们可以用 firefox、safari、opéra 做到这一点吗?

我已经在使用 librairie web-push-php。 (可在此处获取:https://github.com/web-push-libs/web-push-php) 我从文档中复制/粘贴代码,然后替换我的密钥和消息:`

<?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    use Minishlink\WebPush\WebPush;


    $notifications = array(
        array(
            'endpoint' => 'https://updates.push.services.mozilla.com/push/abc...', // Firefox 43+
            'payload' => 'hello !',
            'userPublicKey' => '*****', // base 64 encoded, should be 88 chars
            'userAuthToken' => '*****', // base 64 encoded, should be 24 chars
        ), array(
            'endpoint' => 'https://android.googleapis.com/gcm/send/abcdef...', // Chrome
            'payload' => '{msg:"je fais un test"}',
            'userPublicKey' => '*****',
            'userAuthToken' => null,
        )
    );

    $webPush = new WebPush();

    // send multiple notifications with payload
    foreach ($notifications as $notification) {
        $webPush->sendNotification(
            $notification['endpoint'],
            $notification['payload'], // optional (defaults null)
            $notification['userPublicKey'], // optional (defaults null)
            $notification['userAuthToken'] // optional (defaults null)
        );
    }
    $webPush->flush();

But this code don't work, the page return this :{"multicast_id":6971447349404987967,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}`

我也创建了一个 firebase 帐户,但我不知道如何使用它。

我还想针对通知。 我想发送通知,例如“xxx 对您的帖子发表评论”或“xxx 对您的帖子点赞”或“xxx 向您发送好友请求”

希望你能帮助我。

【问题讨论】:

标签: php push-notification web-push


【解决方案1】:

我从这个页面https://web-push-codelab.appspot.com/ 拿了一把钥匙。但 我不明白他们为什么要提供私钥?

服务器应该生成一个公钥/私钥对。浏览器使用公钥来创建订阅。私钥保留在服务器上,用于加密推送消息。

我还想针对通知。我想发送 “xxx 评论了你的帖子”或“xxx 喜欢”之类的通知 你的帖子”或“xxx 向你发送好友请求”

在某些时候,您让浏览器通过调用pushManager.subscribe 注册订阅(但您的问题中缺少此代码)。这将返回一个解析为订阅的承诺。您应该将此订阅与当前经过身份验证的用户相关联。例如,您可以使用 AJAX 将订阅发送到您的服务器并将其与当前经过身份验证的用户一起存储。每当您想通知此用户时,您都可以使用与此用户关联的订阅。

您可能想看看https://web-push-book.gauntface.com/。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 2023-02-02
    • 2015-08-19
    • 2017-08-12
    • 1970-01-01
    • 2015-11-23
    • 2018-07-01
    • 2012-11-27
    相关资源
    最近更新 更多