Tuesday, 18 October 2016

Push Notification For iOS and Android Using PHP Script

Push Notification For iOS

Push Notification is a message text that pops up on a mobile device Like Android , iOS. We  can send notification any time users don't have to be in the app or using their devices to receive them.

This Php script will showing , How to Send Push Notification in IOS and Android Devices, Be Sure to Change API Access Key. 

Push Notification Script for iOS Devices : 


    $deviceToken = "YOUR DEVICE TOKEN ID";
    $message = "Wow New Notification ... ";
    $ctx = stream_context_create();

    // ck.pem is your certificate file
       stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dash.pem');
       stream_context_set_option($ctx, 'ssl', 'passphrase', '123456');

    // Open a connection to the APNS server
        $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
// Check that we've connected
   if (!$fp)  {
                 die("Failed to connect: $err $errstr");
                  }

                    echo 'Connected to APNS' . PHP_EOL;
        // Create the payload body
          $body['aps'] = array(
                                           'alert' => array(
                                           'body' => $message
                                          ),
                                            'badge' => 1,
                                             'sound' => 'oven.caf',
                                           );
    // Encode the payload as JSON
        $payload = json_encode($body);
    // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));
     if (!$result)
         echo 'Message not delivered' . PHP_EOL;
     else
         echo 'Message successfully delivered' . PHP_EOL;
     // Close the connection to the server
         fclose($fp);

----------------------------------------------------------------------------------------------------------

Push Notification For Android Devices : 


    $deviceToken = "YOUR DEVICE TOKEN ID";
    $msg = array (
                           'badge' => +1,
                            'title'         => 'Wow New Notification ... ',
                            'subtitle'      => 'Php Blog',
                            'tickerText'    => 'Tutorial Section',
                            'vibrate'   => 0,
                            'sound'     => 0,
                            'device_id' => $device_token
                          );

           $fields = array (
                                     'registration_ids'  => $device_token,
                                     'data'                    => $msg
                                    );
           $headers = array (
                                     'Authorization: key=' . API_ACCESS_KEY,
                                     'Content-Type: application/json'
                                      );

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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 ) );
$result = curl_exec($ch );

          print_r($result); // Your Results

----------------------------------------------------------------------------------------------------------


Wednesday, 18 May 2016

How to Calculate Distance between two Lattitude and Longitude in PHP ?



DISTANCE CALCULATE BETWEEN TWO POINT


Want to able to calculate distance between two latitude and longitude ?
Function below to use two latitude and longitude point and calculate distance in
Kilometer , Meter , Yard ,  Miles and Feet ...


function getDistanceBetweenPoints($latitude1, $longitude1, $latitude2, $longitude2) {
        $theta = $longitude1 - $longitude2;
        $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
        $miles = acos($miles);
        $miles = rad2deg($miles);
        $miles = $miles * 60 * 1.1515;
        $feet = $miles * 5280;
        $yards = $feet / 3;
        $kilometers = $miles * 1.609344;
        $meters = $kilometers * 1000;
       
       
        return array('miles' => $miles, 'feet' => $feet, 'yards' => $yards, 'kilometers' => $kilometers, 'meters' => $meters);
    }


CALCULATE DISTANCE FROM QUERY BETWEEN LAT/LONG

If we have already Lat/Long in our Table and want to find out disance from other Lat/Long
Simply we can find Distance From Select Query also its Good Practise for avoid lazy timing.



$user_lattitude = 'xxxx.xxxx';
$user_longitude = 'xxxx.xxxx';
$search_data = array(
                 latitude  => $user_lattitude,
                 longitude => $user_longitude
                    );
                
QUERY :                
SELECT ((ACOS(SIN(' . $this->db->escape($search_data['latitude']) . ' * PI() /180) * SIN(max(latitude) * PI() /180) + COS(' . $this->db->escape($search_data['latitude']) . ' * PI() /180) * COS(max(latitude) * PI() /180) * COS(( ' . $this->db->escape($search_data['longitude']) . ' - max(res.longitude) ) * PI() /180)) *180 / PI()) *60 * 1.1515 *1.609344 ) AS distance,
       res.latitude,
       res.longitude
FROM TABLENAME res

Saturday, 9 April 2016

Find nth highest salary in sql



                                        Find Second Highest Pay of Employee Name


Table Name :-  Emp
         ID
 EMPLOYEE
   PAY
1
JOHN
25000
2
SMITH
28000
3
PHILIP
18000
4
MARKS
21000


Write SQL Query to get Second Highest Pay of Employee name From Above Table :

                                        

SELECT EMPLOYEE

FROM Emp

WHERE Pay NOT IN

(SELECT MAX (Pay) FROM Emp );



----------------------------------------------------------------------------------------------------------------

                                           Find nth Highest Pay of Employee Name 


 Table Name :- Emp
ID
Emp_name
Pay
1
JOHN
25000
2
SMITH
28000
3
PHILIP
18000
4
MARKS
21000
5
HARRY
19050
6
JORDAN
25050




Write a Query For finding nth highest Pay of Employee Name

SELECT Emp_name

FROM Emp outeremp

WHERE (N-1) = (  SELECT COUNT(DISTINCT (inneremp.Pay ) )

                                FROM emp inneremp

                                WHERE inneremp.Pay > outeremp.Pay

    )