Thursday 4 December 2014

Send push notification through php code.

If you are using distribution/ production certificate then use ssl://gateway.push.apple.com:2195

else if you are using developer account then use ssl://gateway.sandbox.push.apple.com:2195

For more detail about push notification read http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1



 $message,
                             'sound' => 'default'
                             );

        // other info which is send to push notification
        $body['others'] =   $otherMsg;

        // finally convert whole message to json and this payload variable send to apple
        $payload = json_encode($body);
        echo $payload."
";

        try {
            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__).'/'.$selectPEM);
            stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
            $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

            $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) .pack('n', strlen($payload)) . $payload;
            $result = fwrite($fp, $msg, strlen($msg));
            fclose($fp);
        } catch(Exception $e) {
            continue;
        }

    }


?>


No comments:

Post a Comment