Push Notification

Sending push notifications to an iOS/Android Application can enhance the user experience exponentially, while allowing you to deliver key information easily. However, sending the push notification to users can be a bit tedious at times, and at times confusing. You need to ensure that you pack your integers, and times correctly – failing to do this and you’ll probably get an unhelpful status from Apple or Google.

I’ve came across some online PHP Scripts for either iOS or Android implementation however not for both. This PHP script includes implementation for both mobile operating systems.

 

class PushNotify {
    private $google_api_url = "https://android.googleapis.com/gcm/send";
    private $google_api_key = "GOOGLE_API_KEY"; /* Google API Console */
    private $ios_certificate = 'xyz.pem'; /* path to your iOS certificate */
    private $ios_passphrase = "123456"; /* your private key's passphrase */

    public function android($registatoin_ids, $options) {
        $fields = array(
            'registration_ids' => array($registatoin_ids),
            'data' => $options,
        );
        $headers = array(
            'Authorization: key=' . $this->google_api_key,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->google_api_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));
        $result = curl_exec($ch);
        curl_close($ch);

        $results = json_decode($result);
        return json_encode(array('status' => (isset($results->success) && $results->success == 1) ? "success" : "fail"));
    }

    public function ios($deviceToken, $options) {
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', $this->ios_certificate);
        stream_context_set_option($ctx, 'ssl', 'passphrase', $this->ios_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);
        //$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
        if (!$fp) {
            //$status = "Failed to connect:" . $err ."=>". $errstr;
            return array('status' => 'fail');
            //echo json_encode(array('status' => $status));
            exit;
        }
        // Create the payload body
        $body['aps'] = array(
            'alert' => $options['message'],
            'sound' => $options['sound']
        );

        // 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));
        $status = ($result) ? 'success' : 'fail';

        // Close the connection to the server
        fclose($fp);
        return array('status' => $status);
        //return json_encode(array('status' => $status));        
    }
}
// send notification details
$options = array(
    "message" => "Test push notification.",
    'title' => 'Test Title',
    'vibrate' => '1',
    'sound' => 'default',
);
$deviceToken = 'device_token';
$send = new PushNotify();
$data = $send->ios($deviceToken, $options);
Posted in Web

Leave a Reply

Your email address will not be published. Required fields are marked *