如何在Firefox中执行Curl向用户发送通知


How to execute Curl to send notification to users in Firefox

下面是我用来执行推送Chrome用户的代码,现在我想通知firefox用户,我知道它现在将通过url作为目标。"https://updates.push.services.mozilla.com/push"

但我不知道该怎么办。

下面提供了适用于Chrome的我的代码。

<?php
    include('header.php');
    define( 'API_ACCESS_KEY', '[API-KEY comes here]' );
    $sql="SELECT * FROM user_data";
    $result = mysqli_query($conn, $sql) or die ('Error'.mysqli_error($conn));
    $registrationIds=array();
    while($row=mysqli_fetch_assoc($result)){
        $registrationIds[] = $row['allow'];
    }
    $ids=json_encode($registrationIds);
    $fields = array
    (
        'registration_ids'  => $registrationIds
    );
    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    curl_close( $ch );
    echo $result;

    ?>

您应该能够像上面一样,将POST发送到Firefox用户的订阅端点。您不需要API密钥。您需要发送的唯一标头是TTL: x,其中x是您希望Mozilla保留消息的秒数,以防我们无法立即发送消息。

可供进一步阅读的良好资源:

  • https://serviceworke.rs/-带有注释源代码的实例
  • https://hacks.mozilla.org/2016/01/web-push-arrives-in-firefox-44/-Push的客户端部分如何工作的描述
  • https://autopush.readthedocs.org/en/latest/http.html-Mozilla推送服务的HTTP API文档
  • https://github.com/mozilla-services/autopush-Mozilla推送服务的源代码

最后,如果您使用的是Firefox 47(目前是开发版),我们有一套全新的工具来调试Service Workers和Push,您可以在about:debugging#workers中找到。