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)
이렇게 공개해 주시니....
언젠쯤 저도 팁으로 보답 드릴 날이 있를지 모르지만,
정말 감사합니다...너무 감사합니다....(ㅡㅡ)(_ _)
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6430 | 12년 전 | 968 | ||
| 6429 | 12년 전 | 9098 | ||
| 6428 | 12년 전 | 593 | ||
| 6427 |
제이티37
|
12년 전 | 1564 | |
| 6426 |
프로프리랜서
|
12년 전 | 1149 | |
| 6425 |
프로프리랜서
|
12년 전 | 1606 | |
| 6424 |
프로프리랜서
|
12년 전 | 1806 | |
| 6423 | 12년 전 | 997 | ||
| 6422 |
퍼블리셔지노군
|
12년 전 | 1569 | |
| 6421 | 12년 전 | 2205 | ||
| 6420 |
|
12년 전 | 1334 | |
| 6419 |
|
12년 전 | 1284 | |
| 6418 | 12년 전 | 1180 | ||
| 6417 |
senseme
|
12년 전 | 7417 | |
| 6416 |
senseme
|
12년 전 | 1324 | |
| 6415 |
senseme
|
12년 전 | 1188 | |
| 6414 |
senseme
|
12년 전 | 1962 | |
| 6413 |
senseme
|
12년 전 | 965 | |
| 6412 |
senseme
|
12년 전 | 1485 | |
| 6411 |
senseme
|
12년 전 | 1088 | |
| 6410 |
senseme
|
12년 전 | 1043 | |
| 6409 |
PASKRAN
|
12년 전 | 2816 | |
| 6408 | 12년 전 | 10796 | ||
| 6407 | 12년 전 | 2989 | ||
| 6406 |
프로프리랜서
|
12년 전 | 2825 | |
| 6405 |
프로프리랜서
|
12년 전 | 1369 | |
| 6404 |
프로프리랜서
|
12년 전 | 2722 | |
| 6403 |
프로프리랜서
|
12년 전 | 816 | |
| 6402 |
프로프리랜서
|
12년 전 | 1725 | |
| 6401 |
프로프리랜서
|
12년 전 | 1139 | |
| 6400 |
프로프리랜서
|
12년 전 | 1461 | |
| 6399 |
프로프리랜서
|
12년 전 | 1179 | |
| 6398 |
프로프리랜서
|
12년 전 | 1479 | |
| 6397 |
프로프리랜서
|
12년 전 | 3600 | |
| 6396 | 12년 전 | 1867 | ||
| 6395 | 12년 전 | 917 | ||
| 6394 |
|
12년 전 | 5236 | |
| 6393 |
제이티37
|
12년 전 | 1198 | |
| 6392 |
songsinica
|
12년 전 | 1196 | |
| 6391 |
|
12년 전 | 3371 | |
| 6390 |
|
12년 전 | 1620 | |
| 6389 | 12년 전 | 4854 | ||
| 6388 |
스누피사랑
|
12년 전 | 5431 | |
| 6387 | 12년 전 | 1838 | ||
| 6386 | 12년 전 | 3056 | ||
| 6385 |
희망과열정
|
12년 전 | 923 | |
| 6384 | 12년 전 | 3287 | ||
| 6383 | 12년 전 | 10594 | ||
| 6382 |
|
12년 전 | 5788 | |
| 6381 |
SugarSkull
|
12년 전 | 1029 | |
| 6380 | 12년 전 | 776 | ||
| 6379 |
|
12년 전 | 1981 | |
| 6378 | 12년 전 | 1559 | ||
| 6377 | 12년 전 | 492 | ||
| 6376 |
후라보노보노
|
12년 전 | 1330 | |
| 6375 | 12년 전 | 2865 | ||
| 6374 | 12년 전 | 829 | ||
| 6373 | 12년 전 | 2367 | ||
| 6372 |
takumi22
|
12년 전 | 1237 | |
| 6371 |
개초보제이
|
12년 전 | 770 | |
| 6370 |
고급자가되고
|
12년 전 | 1456 | |
| 6369 | 12년 전 | 875 | ||
| 6368 | 12년 전 | 3175 | ||
| 6367 | 12년 전 | 1872 | ||
| 6366 | 12년 전 | 2872 | ||
| 6365 |
lilyosia
|
12년 전 | 1790 | |
| 6364 | 12년 전 | 1054 | ||
| 6363 | 12년 전 | 1587 | ||
| 6362 | 12년 전 | 2731 | ||
| 6361 | 12년 전 | 2553 | ||
| 6360 | 12년 전 | 1000 | ||
| 6359 | 12년 전 | 2103 | ||
| 6358 |
|
12년 전 | 1786 | |
| 6357 |
에헤라디야자진방아를돌려라
|
12년 전 | 775 | |
| 6356 | 12년 전 | 2076 | ||
| 6355 | 12년 전 | 3783 | ||
| 6354 | 12년 전 | 3097 | ||
| 6353 | 12년 전 | 1546 | ||
| 6352 | 12년 전 | 7768 | ||
| 6351 | 12년 전 | 2537 | ||
| 6350 | 12년 전 | 2795 | ||
| 6349 | 12년 전 | 1875 | ||
| 6348 | 12년 전 | 3545 | ||
| 6347 | 12년 전 | 1445 | ||
| 6346 | 12년 전 | 1182 | ||
| 6345 | 12년 전 | 1834 | ||
| 6344 | 12년 전 | 1406 | ||
| 6343 |
프로프리랜서
|
12년 전 | 2059 | |
| 6342 |
프로프리랜서
|
12년 전 | 1373 | |
| 6341 |
프로프리랜서
|
12년 전 | 1811 | |
| 6340 |
프로프리랜서
|
12년 전 | 1893 | |
| 6339 |
프로프리랜서
|
12년 전 | 1336 | |
| 6338 |
프로프리랜서
|
12년 전 | 1575 | |
| 6337 | 12년 전 | 5262 | ||
| 6336 |
프로프리랜서
|
12년 전 | 1628 | |
| 6335 |
프로프리랜서
|
12년 전 | 951 | |
| 6334 |
프로프리랜서
|
12년 전 | 1325 | |
| 6333 |
프로프리랜서
|
12년 전 | 2438 | |
| 6332 | 12년 전 | 1097 | ||
| 6331 |
돗단배123
|
12년 전 | 1419 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기