2016-02-08 71 views
4
發送推送消息

我已經實現推送通知瀏覽器,並在當我需要發送推送消息給GCM的地步,我用下面的PHP函數:如何使用PHP捲曲在Firefox

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註冊我的網站才能做到這一點,就像在谷歌?

請幫忙!

回答

4

我自己的解決方案,(一個同事跟我在捲曲請求針對Mozilla bug幫助),現在正在

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和瀏覽器)

+1

您確定它適用於Firefox嗎?我做了同樣的事情,但沒有奏效。當您發送請求時,您從Mozilla服務器獲得的響應格式是什麼? –

+0

是的,現在正在我的應用程序中工作。當我通過CURL(curl -X PUT https://updates.push.services.mozilla.com/push/-browser-endpoint-)測試mozilla服務器並且一切正常時,服務器響應是空的。 –

+0

@WilderBatistaGonzález,在Mozilla中我無法發送推送通知,你能幫我嗎? –