【问题标题】:How use php curl to send push message in Firefox如何使用 php curl 在 Firefox 中发送推送消息
【发布时间】:2023-04-05 23:17:02
【问题描述】:

我已经为 Chrome 实现了推送通知,当我需要向 GCM 发送推送消息时,我使用了以下 PHP 函数:

function send_push_message($subscription_ids){
  // Set GCM endpoint
  $url = 'https://android.googleapis.com/gcm/send';

  $fields = array(
      'registration_ids' => $subscription_ids,
  );

  $headers = array(
      'Authorization: key=API_KEY',
      'Content-Type: application/json'
  );

  $ch = curl_init();

  // Set the url, number of POST vars, POST data
  curl_setopt($ch, CURLOPT_URL, $url);
  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));

  // Execute post
  $result = curl_exec($ch);
  if ($result === FALSE) {
      die('Push msg send failed in curl: ' . curl_error($ch));
  }

  // Close connection
  curl_close($ch);
}

(我之前保存了订阅)

我认为我可以为 Firefox 做这样的事情,使用这个网址:https://updates.push.services.mozilla.com/push

但我不知道我必须做什么。

我需要在 Mozilla 中注册我的网站才能做到这一点,就像在 Google 中一样?

请帮忙!

【问题讨论】:

    标签: php curl push-notification


    【解决方案1】:

    我自己的解决方案(一位同事帮助我解决了我对 mozilla 的 curl 请求中的错误)现在正在运行

    function send_push_message($subscriptionIDs){
    
    if (empty($subscriptionIDs)) return FALSE;
    $chs = $sChrome = array();
    $mh = curl_multi_init();
    foreach ($subscriptionIDs as $subscription){
        $i = count($chs);
        switch ($subscription["browser"]){
            case "firefox":
                $chs[ $i ] = curl_init();
                curl_setopt($chs[ $i ], CURLOPT_URL, "https://updates.push.services.mozilla.com/push/".$subscription["id"] );
                curl_setopt($chs[ $i ], CURLOPT_PUT, TRUE);
                curl_setopt($chs[ $i ], CURLOPT_HTTPHEADER, array( "TTL: 86400" ) );
                curl_setopt($chs[ $i ], CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($chs[ $i ], CURLOPT_SSL_VERIFYPEER, FALSE);
    
                curl_multi_add_handle($mh, $chs[ $i ]);
            break;
            case "chrome":
                $sChrome[] = $subscription["id"];
            break;    
        }
    }
    if (!empty($sChrome)){
        $i = count($chs);
        $chs[ $i ] = curl_init();
        curl_setopt($chs[ $i ], CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
        curl_setopt($chs[ $i ], CURLOPT_POST, TRUE);
        curl_setopt($chs[ $i ], CURLOPT_HTTPHEADER, array( "Authorization: key=MY_KEY", "Content-Type: application/json" ) );
        curl_setopt($chs[ $i ], CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($chs[ $i ], CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($chs[ $i ], CURLOPT_POSTFIELDS, json_encode( array( "registration_ids" => $sChrome ) ) );
        curl_multi_add_handle($mh, $chs[ $i ]);
    }
    
    do {
        curl_multi_exec($mh, $running);
        curl_multi_select($mh);
    } while ($running > 0);
    
    for ($i = 0; $i < count($chs); $i++){
        curl_multi_remove_handle($mh, $chs[ $i ]);
    }            
    
    curl_multi_close($mh);
    }
    

    ($subscriptionIDs 是一个包含 2 个键的数组数组:id 和浏览器)

    【讨论】:

    • 您确定它适用于 Firefox 吗?我也做了同样的事,但没有用。当您发送请求时,您从 Mozilla 服务器获得的响应格式是什么?
    • 是的,现在正在我的应用程序中工作。当我通过 CURL (curl -X PUT updates.push.services.mozilla.com/push/-browser-endpoint-) 测试 mozilla 服务器并且一切正常时,服务器响应为空。
    • @WilderBatistaGonzález,在 Mozilla 中我无法发送推送通知,你能帮帮我吗?
    • 检查我的解决方案的更新,我现在必须将 TTL 标头发送到 Mozilla 服务器。希望对你有所帮助。
    • 如何为firefox和chrome发送1000多个推送通知?
    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多