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개
감사합니다.
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)
게시글 목록
| 번호 | 제목 |
|---|---|
| 26573 | |
| 7309 | |
| 19780 | |
| 7306 | |
| 28025 | |
| 28020 | |
| 28010 | |
| 7304 | |
| 7302 | |
| 7295 | |
| 7292 | |
| 7287 | |
| 19779 | |
| 7284 | |
| 7279 | |
| 7278 | |
| 28009 | |
| 31753 |
그누보드5
그누보드5 강좌 - 갤러리스킨 만들기
|
| 31752 | |
| 7274 | |
| 31751 | |
| 31750 |
그누보드5
그누보드5 강좌 - 네비게이션적용하기
|
| 31749 |
그누보드5
그누보드5 강좌 - 네비게이션적용하기
|
| 31748 |
그누보드5
그누보드5 강좌 - 스킨적용하기
|
| 30979 | |
| 31747 |
그누보드5
그누보드5 강좌 - 홈페이지만들기 실전#2
|
| 28004 | |
| 31745 |
그누보드5
그누보드5 강좌 - 홈페이지만들기 실전
1
|
| 7267 | |
| 7261 | |
| 31742 |
그누보드5
그누보드강좌 샘플
2
|
| 7256 | |
| 19776 |
node.js
mysql 연결 방법 2가지
2
|
| 24650 | |
| 7252 | |
| 7248 | |
| 28002 | |
| 19772 |
JavaScript
한글로 된 숫자를 아라비아 숫자로 변환
3
|
| 7245 | |
| 19770 | |
| 19769 | |
| 30975 | |
| 7243 | |
| 7236 | |
| 28001 | |
| 27994 | |
| 7233 | |
| 7232 | |
| 19768 |
jQuery
제이쿼리로 이미지 회전 시키기
|
| 19765 |
jQuery
구형 익스에서 HTML5 적용시키기
2
|
| 19763 |
jQuery
특정 레이어 높이 100% 잡는 법
1
|
| 30974 | |
| 7224 | |
| 19761 |
jQuery
모바일 해상도에 맞게 이미지 출력
1
|
| 24645 | |
| 19760 | |
| 19758 |
jQuery
제이쿼리를 이용하여 동영상 재생
1
|
| 19757 | |
| 19754 | |
| 19753 |
jQuery
브라우저 상단에 고정 시키는 제이쿼리 소스
|
| 19752 | |
| 19751 |
PHP
php 에러 출력하기
|
| 7223 | |
| 7216 | |
| 7213 | |
| 31741 |
AngularJS
AngularJS 강좌 9. HTML DOM
|
| 19750 |
jQuery
1원팁] css에 hover효과를 제이쿼리로
|
| 19749 |
JavaScript
1원팁]알고 있음 좋은 자바스크립트 함수
|
| 19748 |
jQuery
제이쿼리 기본 효과 메소드
|
| 31740 |
AngularJS
AngularJS 강좌 8. SQL
|
| 31739 |
AngularJS
AngularJS 강좌 7. Tables
|
| 31738 |
AngularJS
AngularJS 강좌 6. XMLHttpRequest
|
| 7208 | |
| 31737 |
AngularJS
AngularJS 강좌 5. Filters
|
| 30967 | |
| 31736 |
AngularJS
AngularJS 강좌 4. Controllers
|
| 7205 | |
| 30965 |
HTML
팁]모바일에서 전화 걸기
1
|
| 30963 | |
| 19747 | |
| 19746 |
기타
viewport 사용
|
| 19745 | |
| 19744 | |
| 19742 | |
| 7193 | |
| 7191 | |
| 31735 |
AngularJS
AngularJS 강좌 3. Directives
|
| 19741 |
jQuery
무한 스크롤 (이미지 갤러리용)
|
| 19740 |
PHP
[알고리즘] 하노이의 탑
|
| 19739 | |
| 7188 | |
| 27986 | |
| 31734 |
AngularJS
AngularJS 강좌 2. Expressions
|
| 31733 |
AngularJS
AngularJS 강좌 1. Introduction
|
| 31732 |
AngularJS
AngularJS 강좌 0. Home
|
| 7186 | |
| 19738 | |
| 19736 |
JavaScript
[알고리즘] 스택(stack)을 이용한 간단 계산기
1
|
| 7180 | |
| 7165 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기