APNS集成在php中发送n个设备

时间:2016-02-22 13:44:31

标签: php ios apple-push-notifications apns-php

我已经尝试使用以下代码为多个设备发送ios推送通知(如果我使用没有循环,它适用于单个设备)。

                $devices = array();// Minimum 2k device ids
                $ctx = stream_context_create();
                // ck.pem is your certificate file

                stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT']."/assets/ck.pem");
                //stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
                stream_context_set_option($ctx, 'ssl', 'passphrase', 'key');
                // 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)
                    exit("Failed to connect: $err $errstr" . PHP_EOL);
                // Create the payload body
                $body['aps'] = array(
                    'alert' => array(
                        'title' => $msg1['title'],
                        'body' => $msg1['message']
                     ),
                    'vibrate'   => 0,
                    'sound' => 'sound'
                );

                $results = array();

                // Encode the payload as JSON
                $payload = json_encode($body);
                // Build the binary notification

                 foreach($devices as $device){ 
                    if(strlen($device) == 64) {
                        $msg = chr(0) . pack('n', 32) . pack('H*', $device) . pack('n', strlen($payload)) . $payload;
                        try {                           
                            $results[] = fwrite($fp, $msg, strlen($msg));
                        }catch (Exception $ex) {
                            // try once again for socket busy error (fwrite(): SSL operation failed with code 1.
                            // OpenSSL Error messages:\nerror:1409F07F:SSL routines:SSL3_WRITE_PENDING)
                            sleep(5); //sleep for 5 seconds
                            $results[] = fwrite($fp, $msg, strlen($msg));
                        }
                     }
                } 

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



        if (empty($results))
            echo 'Message not delivered' . PHP_EOL;
        else
            echo 'Message successfully delivered' . PHP_EOL;

但是这会返回以下错误,请帮助任何人从此

获得成功
A PHP Error was encountered

Severity: Warning

Message: fwrite(): SSL: An existing connection was forcibly closed by the remote host.

Filename: controllers/C_booking.php

Line Number: 265

A PHP Error was encountered

Severity: Warning

Message: fwrite(): SSL operation failed with code 1. OpenSSL Error messages: error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry

Filename: controllers/C_booking.php

Line Number: 265

A PHP Error was encountered

Severity: Warning

Message: fwrite(): SSL: An established connection was aborted by the software in your host machine.

Filename: controllers/C_booking.php

Line Number: 265

0 个答案:

没有答案