开发者

Using CodeIgniter cURL to send a push notification via UrbanAirship

I have an urbanairship account set up to send push notifications to my app. This is all working, but now I'm trying to integrate it with my codeigniter admin site so that push notifications are sent by UA and stored to a database in one step. I'm trying to use the cURL library and following the UA API documentation,(http://urbanairship.com/docs/push.html), but each time I get a 404 error. However if I take the cURL lines out, the data is added to the database fine, (so it is receiving the data correctly from the form).

Here's the function in my controller:

function saveAnnouncement() {

  $this->load->helper('html');
  $this->l开发者_如何学Coad->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');

}

I'm new to codeigniter, curl and urbanairship, so as you can imagine this is a bit of a nightmare. Will be grateful for any help available!

Thanks!


Have you set your curl config to trust the ssl certificate of the site you are trying to connect? First try with this

 curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); 

and if it works - that's the problem. Then you should setup your curl connection properly adding particular certificate.


This code in PHP, Urbanairship, curl worked for me:

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); 


Just use normal curl if you are not familiar with Codeigniter !

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";

    }
}

Ref : http://blog.jamesbaca.net/?p=385

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜