스마트폰/아이폰

apple push notification service php 버전

나를찾는아이 2011. 1. 26. 11:30
728x90
반응형
$sandbox = false;

$alertText = '푸시 보낼 내용';
$deviceToken = '디바이스토큰';

$msg = array();
$msg['alert'] = $alertText;
$msg['badge'] = 1; // 배지(선택적)
$msg['sound'] = 'default'; // 사운드(선택적)
$body['aps'] = $msg;
$body['message_id'] = 7027; // 추가 파라메터
$payload = json_encode($body); // payload의 최대사이즈는 256byte

if($sandbox){
    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsCert = '개발용 인증서 경로';
}else{
    $apnsHost = 'gateway.push.apple.com';
    $apnsCert = '배포용 인증서 경로';
}

$apnsPort = 2195;

$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);
if(!$apns) echo 'Fail to connection:'.$error.':'.$errorString;

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

fwrite($apns, $apnsMessage);

fclose($apns);




APNS(apple push notification service)의 php 버전입니다.

$sandbox를 false로 하는 경우 배포용 인증서로 푸시를 발송합니다.

인증서는 .p12 확장자의 파일이 아닌 .pem 파일이어야 합니다.

한글로 주석도 첨부하였으니 푸시기능 구현하시는 분들께 도움이 되었으면 좋겠네요.


728x90
반응형