ios消息推送,php做服务端



ios 消息推送,php做推送服务端。服务端代码如下 

<?php

           // 得到deviceToken

           $deviceToken = 'c3d475905aafdf800ddfce96973efa4679bc87ab2c';

            // 密钥:

            $passphrase = 'pass123';

            // 要推送的消息:

            $message = 'My first push test!';

            //默认声音
            $sound = 'default';

            $ctx = stream_context_create();
            
            // ck.pem文件 由ios提供
            stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');

            stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

            // 打开一个连接到apns服务器
            $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);
                 echo 'Connected to APNS' . PHP_EOL;
            $body['aps'] = array(
	                  'alert' => $message,
	                  'sound' => 'default',
                          "badge" => $count
	                   );
            // 转换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));
            if (!$result)
	        echo 'Message not delivered' . PHP_EOL;
            else
	        echo 'Message successfully delivered' . PHP_EOL;
             // Close the connection to the server
             fclose($fp);    
?>

猜你喜欢

转载自peasant.iteye.com/blog/1762678