btmanikandan
btmanikandan

Reputation: 1931

Pushnotification for ios via PHP Server

I like to implement in my IOS application. I have already created certificate for push notification and pushkey.pem when I have implement it from my mac I have received push notification in the device but when i try to implement it from my server I am getting the following error I don't know how to over come this issue please suggest me some idea to solve this issue

Errors

1)Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)

2)Warning: fwrite(): supplied argument is not a valid stream resource

3)Warning: fclose(): supplied argument is not a valid stream resource

Upvotes: 0

Views: 605

Answers (3)

SuReSh
SuReSh

Reputation: 1511

PHP CODE.. FOR IOS PUSH NOTIFICATION

to send push notification call this function and pass device id and message you send in pushnotificaion...

function pushnotification_ios($device_token, $message){

        $passphrase = "Your_PEM_File_Password_Here";

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', YOUR_PEM_FILE_PATH_HERE);
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    // use this when you in sandbox mode..
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    // use this when you in development mode..
    //  $fp = stream_socket_client(
    //  'ssl://gateway.push.apple.com:2195', $err,
    //$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        $body['aps'] = array(
            'badge' => $badge,
            'alert' => $message,
            'sound' => 'default',
            'content-available' => '1'
        );
    //echo "<pre>"; print_r($body);
        $payload = json_encode($body);

        $msg = chr(0) . pack('n', 32) . pack('H*', $device_token) . pack('n', strlen($payload)) . $payload;

        $result = fwrite($fp, $msg, strlen($msg));  //echo "<pre>"; print_R($result);
        /*
        if (!$result){
            $data = array(
                'Message' => 'Message not delivered' . PHP_EOL
                );
            }  else {
        $data = array(
                'Message' => 'Message successfully delivered' . PHP_EOL
                );
        } //echo "<pre>"; print_R($result); */

        fclose($fp);
    return $result;
    }

Upvotes: 0

Antosha
Antosha

Reputation: 1

see php-apn extension to PHP

Apn is a PHP extension to introduce simple yet powerful interface for sending push notifications to iOS and OS X devices from within your PHP code

http://pecl.php.net/package/apn

Upvotes: 0

Albin Joseph
Albin Joseph

Reputation: 1141

Hi friend please refer the tutorial http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 They explained all about push notification and how to implement.

You also get the iOS and PHP sample code.

Upvotes: 6

Related Questions