본문 바로가기

iPhone dev./Push관련

[iPhone 개발] Push 구현(Mac에서 PHP로)

Certificate와 관련해서는 다음을 참조:
http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/

PHP 코드와 관련해서는 다음을 참조:
<html>

<?php
echo "Sending push message...";

$strtotime = strtotime($question['question_regist']);
$alertText = "PUSH로 보낸 메시지.";
$deviceToken = "XXXXXXXX 61b3b37f edb9530c XXXXXXXX 1fff1de7 ac1a8b3e 2513847f 11d6aac3";

$msg = array();
$msg['alert'] = array('body'=>$alertText, 'action-loc-key'=>"세부정보");
$msg['badge'] = 312321;
$msg['sound'] = "default";
$tmp = array('aps' => $msg);
$payload = json_encode($tmp);

//세팅
$apnsHost = 'gateway.sandbox.push.apple.com'; //리얼서버는 gateway.push.apple.com
$apnsPort = 2195;
$apnsCert = 'test.pem'; //인증파일

//소켓 생성
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
//(에러는 $errorString 조회)

//전송
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

//마무리
//socket_close($apns);
fclose($apns);
?>
Done.
</html>