Push notifications are messages that allow an app to notify of a message similar to how a text message pops up on your screen with a sound.
Code for Android ,Iphone API push notification is given below by using urbanairship.
1.Android Push Notification
$apiKey = 'XXXXXXXXX';
// Device Token
$registrationIDs = 'XXXXXXXXXXX';
// Message that is to be send in notification
$message ='Hello World!';
// Additional parameter if need to be send
$test = 'test';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $registrationIDs,'data' => array("message" => $message,"test"=>$test),);
$headers = array('Authorization: key=' . $apiKey,'Content-Type: application/json');
// Open connection
$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);
// Close connection
curl_close($ch);
$err_chk = json_decode($result);
2.Iphone Push Notification
define('APPKEY','XXXXXXXXXXXXXXX');
define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
$contents = array();
$contents['badge'] = "+1";
$contents['sound'] = "default";
$contents['alert'] = 'Hello World !';
$devices ='XXXXXXXXXXXXXXXXXXXXXXXX'; // Device Token
$push = array("aps" => $contents);
$push['device_tokens'] = $devices;
$json = json_encode($push);
$session = curl_init("https://go.urbanairship.com/api/push/");
curl_setopt($session, CURLOPT_USERPWD, "MG-TojJETtSOKrsq0kvJoA:-6LecLj3RWeAouYlJUr-YQ");
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_SSLVERSION,3);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$content = curl_exec($session);
$response = curl_getinfo($session);
if($response['http_code'] != 200)
{
echo "Got negative response from server, http code: ".
$response['http_code'] . "\n";
}
else
{
echo "This worked!";
}
curl_close($session);
Happy Coding ! :)
0 Comment(s)