drhg24275 2017-06-07 20:28
浏览 40
已采纳

用PHP推送通知。 如何使用它?

i would like to send push notification in PHP, but i don't know how to do.

I successfully create the Service Worker.

I took a key from this page https://web-push-codelab.appspot.com/. But i don't understand why do they give a private key ?

Then i test my push notifiaction, and i send some notification from this page. But I would like to send push notification from my PHP request. More, i would this work on all the bowsers possible, this code works just with google chrome. Can we do this with firefox, safari, opéra ?

I already use the librairie web-push-php. (avaible here: https://github.com/web-push-libs/web-push-php) I copy/pase the code from the documentation, and i remplace my key, and messages:`

<?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"}]}`

I also create a firebase account, but i don't know how to usi it.

And i would like also target the notifications. I would like to send the notification like "xxx commented to your post" or "xxx liked to your post" or "xxx send to you a friend request"

Hope, you can help me.

  • 写回答

1条回答 默认 最新

  • douangoo48863 2017-06-07 20:37
    关注

    I took a key from this page https://web-push-codelab.appspot.com/. But i don't understand why do they give a private key ?

    The server is supposed to generate a public/private keypair. The public key is used by the browser to create a subscription. The private key remains on the server and is used to encrypt the push message.

    And i would like also target the notifications. I would like to send the notification like "xxx commented to your post" or "xxx liked to your post" or "xxx send to you a friend request"

    At some point you have the browser register a subscription by calling pushManager.subscribe (but this code is missing from your question). This returns a promise that resolves to a subscription. You should associate this subscription to the currently authenticated user. For example, you can use AJAX to send the subscription to your server and store it with the currently authenticated user. Whenever you want to notify this user you can use the subscription that is associated with this user.

    You might want to take a look at https://web-push-book.gauntface.com/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上