2011-06-19 88 views
4

我有一個urbanairship帳戶,用於向我的應用發送推送通知。這是所有工作,但現在我試圖將其與我的codeigniter管理網站集成,以便推送通知由UA發送並一步存儲到數據庫中。我正在嘗試使用cURL庫並遵循UA API文檔(http://urbanairship.com/docs/push.html),但每次遇到404錯誤。但是,如果我將cURL行取出,數據將被添加到數據庫中,因此它正在從表單中正確接收數據。使用CodeIgniter cURL通過UrbanAirship發送推送通知

下面是我的控制器的功能:)

功能saveAnnouncement({

$this->load->helper('html'); 
    $this->load->library('session'); 
    $this->load->helper('json'); 
    $this->load->library('curl'); 

    $new_announcement = $this->input->post('announcement_text'); 

    if($this->session->userdata('logged_in') != true) 
    { 
     redirect('/admin/login'); 
    } 

    $this->curl->create('https://go.urbanairship.com/api/push/broadcast/'); 
    $this->curl->http_login('<application key>', '<master secret>'); 
    $announcement_push = array('aps'=>array('alert'=>$new_announcement, 'sound'=>'default')); 
    $announcement_push['encoded_data'] = json_encode($announcement_push); 
    $this->curl->post($announcement_push); 
    $this->curl->execute(); 

    $this->load->model('Announcements'); 
    $this->Announcements->Add($new_announcement); 
    redirect('/admin/announcements'); 

}

我是新來的笨,捲曲和urbanairship,所以你可以想象這是一個噩夢。將感謝任何幫助!

謝謝!

+0

我開始笨庫urbanairship這裏:https://github.com/caioiglesias/Urban-Airship-CodeIgniter-library –

+0

現在,他們想要錢!沒有更多的免費推! –

回答

1

您是否已將捲曲配置設置爲信任您嘗試連接的站點的ssl證書?先試試這個

curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); 

如果它工作 - 這就是問題所在。然後,您應該正確設置捲曲連接,添加特定的證書。

+0

感謝您的建議。不幸的是,它仍然給我404錯誤:S – Andy

0

這段代碼在PHP,Urbanairshipcurl工作對我來說:

define ('URBAN_APP_MASTERKEY', XXXXXX); 
define ('URBAN_APIKEY',XXXXX); 
define ('URBAN_APP_SECRETKEY',XXXXXX); 
define('PUSHURL', 'https://go.urbanairship.com/api/push/'); 

$contents = array(); 
$contents['badge'] = "1"; 
$contents['alert'] = "Howdy, doody"; 
$contents['sound'] = "cat.caf"; 

$devices = array('device_tokens'); 

$push = array("aps" => $contents); 
$push['device_tokens'] = $devices; 

$json = json_encode($push); 
$url = PUSHURL; 
echo $json; //display the actual content 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERPWD, URBAN_APIKEY . ':' . URBAN_APP_MASTERKEY); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "$json"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 

$output = curl_exec($ch); 

if($response['http_code'] != 200) 
{ 
    echo "Got negative response from server, http code: ". 
    $response['http_code'] . "\n"; 
} 
else 
{ 
    echo "Wow, it worked!\n"; 
} 

curl_close($ch); 
+0

這會讓你發送到個人設備? $ devices是phoneID的數組? – Siriss

+0

@Siriss yes $ devices是一組電話號碼。它會發送到個人設備 –

+0

謝謝Darshit-美元設備如何填充?我被困在這個問題上。 – Siriss

0

只需使用正常捲曲,如果你不熟悉笨!

public function pushNotification(){ 
    // Urban AirShip Doc : http://docs.urbanairship.com/connect/android_push.html 

    // create the contents of the android field 
    // sample JSON => {"audience": "all", "notification": {"alert": "Hello!"}, "device_types": ["android"]} 

    $android = array(); 
    $android['audience'] = "all"; 
    $android['notification'] = array('alert'=>'Hello !'); 
    $android['device_types'] = array("android"); 

    // convert the dictionary to a json string 
    $data = json_encode($android); 

    // open connection 
    $ch = curl_init(); 

    // the url and credentials for posting to urban airship 
    $url = 'https://go.urbanairship.com/api/push/'; 
    $username = "YourAPIkey"; 
    $password = "YourMasterSecretKey"; 

    // set the url, number of POST vars, POST data 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/vnd.urbanairship+json; version=3;')); 
    curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // execute post 
    $result = curl_exec($ch); 
    $arrayResult=json_decode($result); 

    // close connection 
    $res = curl_close($ch); 

    if($arrayResult->ok == 1){ 
      print "Success"; 
    } else { 
      print "Error"; 

    } 
} 

編號:http://blog.jamesbaca.net/?p=385