GCM은 되는데 APNS는 테스트 못해봤네요.
[code]
<?php
class TBPush {
var $config = array();
# 설정
public function set($type='', $key='') {
$this->config[$type] = $key;
}
# 설정 확인
public function config() {
return $this->config;
}
# 메시지 전송
public function send($device=array(), $subject='', $message='', $option=array()) {
if(is_array($device['gcm'])) $this->GCM($device['gcm'], $subject, $message, $option);
if(is_array($device['apns'])) $this->APNS($device['apns'], $subject, $message, $option);
}
# GCM 발송
private function GCM($device, $subject='', $message='', $option=array()) {
// 초기 값
$data = array();
// 통신 URL
$url = 'http://android.googleapis.com/gcm/send';
// 클래스 설정값 변수화
$config = $this->config;
// 조건확인
if(!trim($config['gcm'])) $this->error('[gcm] 설정 값 없음');
if(!trim($message)) $this->error('[gcm] 전달할 메시지가 없습니다.');
if(!is_array($device) || count($device) == 0) $this->error('[gcm] DEVICE 설정 값 오류');
// 전달내용 조합
$data['registration_ids'] = $device;
if(trim($subject)) $data['data']['subject'] = $subject;
$data['data']['message'] = $message;
// 통신 해더 생성
$headers = array(
'Authorization: key=' . $config['gcm'],
'Content-Type: application/json'
);
// 통신 처리
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
// 결과 반환
return '[gcm] '.$result;
}
# APNS 발송 (http://qnibus.com/blog/how-to-develop-service-provider/#comment-512 참조)
private function APNS($device, $subject='', $message='', $option=array()) {
// 옵션값 변수화
/*
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9
*/
$mode = $option['mode'];
$badge = $option['badge']?(int)$option['badge']:1; // 지정뱃지
$sound = $option['sound']; // 지정 사운드
$acme1 = $option['acme1']; // 커스텀 변수1
$acme2 = $option['acme2']; //커스텀 변수2
// 통신 URL
if($mode == 'test') $url = 'ssl://gateway.sandbox.push.apple.com:2195';
else $url = 'ssl://gateway.push.apple.com:2195';
// 클래스 설정값 변수화
$config = $this->config;
// 조건확인
if(!is_file($config['apns'])) $this->error('[apns] 인증서 파일이 존재하지 않습니다.');
if(!is_array($device) || count($device) == 0) $this->error('[apns] DEVICE 설정 값 오류');
if(!trim($message)) $this->error('[apns] 전달할 메시지가 없습니다.');
// 설정값
$result = '';
$data = array();
$data['aps'] = array();
$data['aps']['arert'] = array();
$data['aps']['arert']['body'] = $message;
$data['aps']['badge'] = $badge;
if($sound) $data['aps']['sound'] = $sound;
if($acme1) $data['acme1'] = $acme1;
if($acme2) $data['acme2'] = $acme2;
// 디바이스(토큰) 수 만큼 전송
for($i=0; $i<count($device); $i++) {
$msg = '';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $config['apns']);
$fp = stream_socket_client($url, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp) $this->error("[apns] Failed to connect $err $errstr\n");
$payload = json_encode($data);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $device[$i])) . pack("n",strlen($payload)) . $payload;
$result .= "[apns] Sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
}
return $result;
}
public function error($msg){
echo "ERROR:";
echo "\t" . $msg;
}
}
[/code]
사용법
[code]
include_once(APP_PATH.'/class/TBPush.class.php');
$TBPush = new TBPush;
$TBPush->set('gcm', 'GCM 키');
$TBPush->set('apns', '키파일 위치');
$device = array('gcm'=>array('디바이스코드1', '디바이스코드2'), 'apns'=> array('토큰', '토큰2'));
echo $TBPush->send($device, '제목', '내용');
[/code]
ps. apns쪽에 잘 발송되는지 부탁 드립니다 ㅎㅎ
댓글 4개
지금 하는 작업 다 끝내놓고, 하이브리드할때 Terrorboy 님 TIP 들만 찾아서 많이 배우겠습니다.
TIP 감사합니다.
감사합니다.
http://blog.terrorboy.net/entry/%EC%9B%B9%EC%86%8C%EC%BC%93%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-%EB%A1%9C%EA%B7%B8-%ED%99%95%EC%9D%B8%EA%B8%B0
이건 이제 필수인 유틸리티 프로그램이구요 ㅎㅎㅎ
네이티브 통신 할때 웹쪽 처리 하는 과정중 앱에서 전달받은 값을 확인 하기 위한.... ㅎㅎㅎ
ps. 참고로 본문의 클래스는 네이티브+하이브리드 공용입니다 ㅎㅎ
앱요청->서버->처리->앱으로 반환(xml)
이렇게 공개해 주시니....
언젠쯤 저도 팁으로 보답 드릴 날이 있를지 모르지만,
정말 감사합니다...너무 감사합니다....(ㅡㅡ)(_ _)
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6130 |
gender
|
12년 전 | 1098 | |
| 6129 | 12년 전 | 1322 | ||
| 6128 |
|
12년 전 | 3464 | |
| 6127 | 12년 전 | 697 | ||
| 6126 |
|
12년 전 | 2210 | |
| 6125 |
|
12년 전 | 4887 | |
| 6124 | 12년 전 | 663 | ||
| 6123 | 12년 전 | 3846 | ||
| 6122 | 12년 전 | 1018 | ||
| 6121 | 12년 전 | 3754 | ||
| 6120 | 12년 전 | 910 | ||
| 6119 | 12년 전 | 1772 | ||
| 6118 | 12년 전 | 932 | ||
| 6117 | 12년 전 | 2152 | ||
| 6116 | 12년 전 | 7944 | ||
| 6115 | 12년 전 | 1923 | ||
| 6114 |
|
12년 전 | 1708 | |
| 6113 | 12년 전 | 1526 | ||
| 6112 | 12년 전 | 595 | ||
| 6111 | 12년 전 | 2112 | ||
| 6110 | 12년 전 | 1851 | ||
| 6109 | 12년 전 | 643 | ||
| 6108 | 12년 전 | 1201 | ||
| 6107 | 12년 전 | 625 | ||
| 6106 | 12년 전 | 906 | ||
| 6105 | 12년 전 | 1194 | ||
| 6104 | 12년 전 | 3596 | ||
| 6103 | 12년 전 | 2126 | ||
| 6102 | 12년 전 | 2316 | ||
| 6101 | 12년 전 | 3678 | ||
| 6100 | 12년 전 | 3485 | ||
| 6099 | 12년 전 | 3157 | ||
| 6098 | 12년 전 | 4027 | ||
| 6097 | 12년 전 | 1017 | ||
| 6096 | 12년 전 | 5986 | ||
| 6095 | 12년 전 | 1371 | ||
| 6094 | 12년 전 | 1226 | ||
| 6093 | 12년 전 | 3411 | ||
| 6092 | 12년 전 | 3053 | ||
| 6091 | 12년 전 | 5196 | ||
| 6090 | 12년 전 | 2706 | ||
| 6089 | 12년 전 | 3328 | ||
| 6088 | 12년 전 | 1016 | ||
| 6087 | 12년 전 | 848 | ||
| 6086 | 12년 전 | 2020 | ||
| 6085 |
|
12년 전 | 808 | |
| 6084 |
웹디자인되고파
|
12년 전 | 2222 | |
| 6083 | 12년 전 | 1510 | ||
| 6082 | 12년 전 | 1111 | ||
| 6081 | 12년 전 | 2101 | ||
| 6080 |
Stiven
|
12년 전 | 2312 | |
| 6079 |
프로프리랜서
|
12년 전 | 1323 | |
| 6078 |
프로프리랜서
|
12년 전 | 789 | |
| 6077 |
프로프리랜서
|
12년 전 | 1346 | |
| 6076 |
프로프리랜서
|
12년 전 | 834 | |
| 6075 |
프로프리랜서
|
12년 전 | 1224 | |
| 6074 | 12년 전 | 3856 | ||
| 6073 | 12년 전 | 3950 | ||
| 6072 | 12년 전 | 1378 | ||
| 6071 | 12년 전 | 6994 | ||
| 6070 | 12년 전 | 7576 | ||
| 6069 | 12년 전 | 2323 | ||
| 6068 | 12년 전 | 3870 | ||
| 6067 |
smwkd
|
12년 전 | 633 | |
| 6066 | 12년 전 | 3660 | ||
| 6065 | 12년 전 | 3486 | ||
| 6064 | 12년 전 | 2720 | ||
| 6063 | 12년 전 | 2841 | ||
| 6062 | 12년 전 | 2372 | ||
| 6061 | 12년 전 | 2281 | ||
| 6060 | 12년 전 | 5186 | ||
| 6059 | 12년 전 | 2818 | ||
| 6058 | 12년 전 | 3137 | ||
| 6057 | 12년 전 | 2263 | ||
| 6056 | 12년 전 | 6842 | ||
| 6055 | 12년 전 | 2582 | ||
| 6054 | 12년 전 | 3437 | ||
| 6053 | 12년 전 | 2330 | ||
| 6052 | 12년 전 | 4824 | ||
| 6051 | 12년 전 | 3713 | ||
| 6050 | 12년 전 | 2557 | ||
| 6049 | 12년 전 | 2246 | ||
| 6048 |
|
12년 전 | 1300 | |
| 6047 | 12년 전 | 3441 | ||
| 6046 | 12년 전 | 4096 | ||
| 6045 | 12년 전 | 3458 | ||
| 6044 | 12년 전 | 5333 | ||
| 6043 | 12년 전 | 1652 | ||
| 6042 | 12년 전 | 1282 | ||
| 6041 | 12년 전 | 5165 | ||
| 6040 | 12년 전 | 942 | ||
| 6039 | 12년 전 | 3414 | ||
| 6038 | 12년 전 | 3415 | ||
| 6037 | 12년 전 | 2995 | ||
| 6036 | 12년 전 | 3343 | ||
| 6035 | 12년 전 | 2867 | ||
| 6034 | 12년 전 | 2846 | ||
| 6033 | 12년 전 | 2868 | ||
| 6032 | 12년 전 | 2854 | ||
| 6031 | 12년 전 | 2883 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기