php - 如何在 Firefox 中使用 php curl 发送推送消息

标签 php curl push-notification

我已经为 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 做这样的事情,使用这个 url:https://updates.push.services.mozilla.com/push

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

我需要像在 Google 中那样在 Mozilla 中注册我的网站吗?

请帮忙!

最佳答案

我自己的解决方案(一位同事帮助我解决了对 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 是一个包含两个键的数组:id 和 browser)

关于php - 如何在 Firefox 中使用 php curl 发送推送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271224/

相关文章:

java - 加载curl php页面时如何获取java生成的元数据

php - https、Jquery、PHP、Curl 和 document.cookie

使用 TLS 证书与使用身份验证 token 的 iOS 推送通知

c# - 如何在 C# .Net 上发送批量推送通知 Android

ios - 使用 Cordova Push phonegap-plugin-push 1.8.0 的 PhoneGap Windows 构建失败

PHP + MySQL + HTML 在表格中显示不同的信息

php - 自定义 WooCommerce 电子邮件通知中的电子邮件 header

php - 为 MySQLi 查询执行设置超时

php - 使用应用程序文件安装 EasyPHP 和 MySQL 数据

php - 使用sftp上传文件时如何修复PHP curl错误CURLE_SSH(79)