Skip to content

Instantly share code, notes, and snippets.

@oussaki oussaki/push.php
Created Jun 8, 2016

Embed
What would you like to do?
<?php session_start();
$connect = mysql_connect( "localhost", '******', "******");
mysql_select_db("******");
$url = 'https://android.googleapis.com/gcm/send';
$headers = array(
'Authorization: key=' . '******',
'Content-Type: application/json'
);
$registrationIDs[] = '';
// IN CASE OF BROADCAST Notification
if (isset($_GET['sendBroadCastUpdate'])) {
if (isset($_GET['update_id'])) {
$update_id = $_GET['update_id'];
$randomNum = rand(10,1000);
$arrData = array(
'notId' => $randomNum ,
"title"=> 'New updates from MBR Headhunters' ,
"message" => $_GET['description'] ,
"update_id" => $update_id ,
'typenotification' => 'newUpdate' );
// collect all the ids and put them in an array ....
$sql_collection = "select registrationid from account "; //where owner = 0
if($query_run = mysql_query($sql_collection)) {
$gcmRegIds = array();
while($query_row = mysql_fetch_assoc($query_run)) {
if ($query_row['registrationid'] != "") {
array_push($gcmRegIds, $query_row['registrationid']);
}
}
print_r($gcmRegIds);
if (count($gcmRegIds)>0) {
$fields = array(
'registration_ids' => $gcmRegIds,
'data' => $arrData,
'delay_while_idle'=> false,
'time_to_live' => 86400,
'collapse_key'=>"".$randomNum.""
);
SendGCM($url,$headers,$fields);
}
}
}
}
if (isset($_GET['id']) && isset($_GET['server']) ) {
$account_id = $_GET['id'];
$ser = $_GET['server'];
$server = "http://******.com/api/";
// GET his registration_id from db
$sql = "select registrationid from account where id = $account_id";
$result = mysql_query($sql);
if ($result) {
$msg_counter = 0;
$data = mysql_fetch_assoc($result);
if ($data["registrationid"] != '') {
$registrationIDs[] = $data["registrationid"];
// GET ALL Notifications types , candidate or recruiter whatever
// push it if possible
// 1 new messages candidates or recruiter ...
$call = CallMbrAPI($server."new/messages/".$account_id);
$data = json_decode($call,true);
if ($data['status'] && $data['count'] > 0) {
for ($i=0; $i < count($data['messages']) ; $i++) {
$message = $data['messages'][$i]['message'];
// $sender = $data['messages'][$i]['sender'];
$application_id = $data['messages'][$i]['application_id'];
$typenotification = "newmessages";
$randomNum = rand(10,1000);
$arrData = array(
'notId' =>$randomNum,
"title"=> 'New message',
"message" => $message,
"sender" => $data['messages'][$i]['sender'] ,
"receiver" => $data['messages'][$i]['receiver'] ,
"server" => $ser,
'application_id' => $application_id,
'typenotification' => $typenotification);
$fields = array(
'registration_ids' => $registrationIDs,
'data' => $arrData,
'delay_while_idle'=> false,
'time_to_live' => 86400,
'collapse_key'=>"".$randomNum.""
);
SendGCM($url,$headers,$fields);
$msg_counter++;
}
}
$call = CallMbrAPI($server."candidate/reminder/interview/".$account_id);
$data = json_decode($call,true);
if ($data['status'] && $data['count'] > 0) {
for ($i=0; $i < count($data['interviews']) ; $i++) {
$typenotification = "candidatereminder";
$message = "interview at : " . $data['interviews'][$i]['interview'];
$randomNum = rand(10,1000);
$arrData = array( "title"=> 'You have an interview in MBR Today',
'notId' => $randomNum,
"message" => $message,
'typenotification' => $typenotification);
$fields = array(
'registration_ids' => $registrationIDs,
'data' => $arrData,
'delay_while_idle'=> false,
'time_to_live' => 86400,
'collapse_key'=>"".$randomNum.""
);
SendGCM($url,$headers,$fields);
$msg_counter++;
}
}
// echo $call;
$call = CallMbrAPI($server."recruiter/reminder/interview/".$account_id);
$data = json_decode($call,true);
if ($data['status'] && $data['count'] > 0) {
for ($i=0; $i < count($data['interviews']) ; $i++) {
$typenotification = "recruiterreminder";
$message = "interview at : " . $data['interviews'][$i]['interview'];
$randomNum = rand(30,88);
$arrData = array( "title"=> 'You have an interview with '. $data['interviews'][$i]['fname'],
"message" => $message,
'notId' => $randomNum,
'typenotification' => $typenotification);
$fields = array(
'registration_ids' => $registrationIDs,
'data' => $arrData,
'delay_while_idle'=> false,
'time_to_live' => 86400,
'collapse_key'=>"".$randomNum.""
);
SendGCM($url,$headers,$fields);
$msg_counter++;
}
}
// echo $call;
$call = CallMbrAPI($server."new/appliers/notification/".$account_id);
echo $call;
$data = json_decode($call,true);
if ($data['status'] && $data['count'] > 0) {
for ($i=0; $i < count($data['list']) ; $i++) {
$typenotification = "newApplier";
$message = "New candidate for : " . $data['list'][$i]['title'];
$application_id = $data['list'][$i]['application_id'];
$job_id = $data['list'][$i]['job_id'];
$randomNum = rand(10,1000);
$arrData = array( "title"=> 'New candidate ',
"message" => $message,
"job_id" => $job_id,
"server" => $ser ,
'notId' => $randomNum,
"application_id" => $application_id,
'typenotification' => $typenotification );
$fields = array(
'registration_ids' => $registrationIDs,
'data' => $arrData,
'delay_while_idle'=> false,
'time_to_live' => 86400,
'collapse_key'=>"".$randomNum.""
);
// echo "<br> appliers";
SendGCM($url,$headers,$fields);
$msg_counter++;
}
}
// echo $call;
}
}
if ($msg_counter == 0) {
echo "{'******':'******'}";
}
}else {
echo "{'******':'******'}";
}
function CallMbrAPI($url){
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
// curl_setopt( $ch, CURLOPT_GET, 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);
return $result;
}
function SendGCM($url,$headers,$fields){
// 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);
echo $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.