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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64