PHP

How to send push notification to I-Phone and Android

Please check below code which will help you to send push notifications


include('pushnotification.php');

$msg = array( 'mtitle' =>'Push notification title', 'mdesc' => 'Push notification desc');

// For Android

$regId = 'adadadadA8686865665656565dddsxsx;

// For iOS

$deviceToken = 'FFFFFFFFFFSDSDDSD';

// For Android

PushNotifications::android($msg, $token);

// For iOS

PushNotifications::iOS($msg, $token);

Include a class file name as pushnotification.php


class PushNotifications {
// (Android)API access key from Google API's Console.
private static $API_ACCESS_KEY = 'sdsdsdsdsdsdd-gfgerrccscs';
// (iOS) Private key's passphrase.
private static $passphrase = 'dsefsdd';
public function __construct() {
    exit('Init function is not allowed');
}

// Sends Push notification for Android users
public function android($data, $reg_id) {
    $url = 'https://android.googleapis.com/gcm/send';
    $message = array(
    'title' => $data['mtitle'],
    'message' => $data['mdesc'],
    'subtitle' => '',
    'tickerText' => '',
    'msgcnt' => 1,
    'vibrate' => 1
    );

    $headers = array(
    'Authorization: key=' .self::$API_ACCESS_KEY,
    'Content-Type: application/json'
    );

    $fields = array(
    'registration_ids' => array($reg_id),
    'data' => $message,
    );

    return $this->useCurl($url, $headers, json_encode($fields));
}

public function iOS($data, $devicetoken) {
    $deviceToken = $devicetoken;
    $ctx = stream_context_create();
    // ck.pem is your certificate file
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);

    // Open a connection to the APNS server
    $fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);

    // Create the payload body
    $body['aps'] = array(
    'alert' => array(
    'title' => $data['mtitle'],
    'body' => $data['mdesc'],
    ),
    'sound' => 'default'
    );

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

    // Close the connection to the server
    fclose($fp);

    if (!$result)
        return 'Message not delivered' . PHP_EOL;
    else
        return 'Message successfully delivered' . PHP_EOL;

}

// Curl
private function useCurl(&$model, $url, $headers, $fields = null) {
    // Open connection
    $ch = curl_init();
    if ($url) {
        // 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);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        if ($fields) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        }

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        return $result;
    }
}
 
}