TIP게시판

제목 parse push전송 클래스
글쓴이 테러보이 작성시각 2015/05/04 13:29:30
댓글 : 5 추천 : 0 스크랩 : 0 조회수 : 12680   RSS
테러보이
클래스도 급만들고 어플도 급 만들었네요... 
<?php
/**
* 제작: 임동덕
* 작업일: 2015-05-04
* 사용법
-------------------------------------------
$Push = new ParsePush; // 클래스 선언
$Push->setConfig( parse 앱아이디 , parse rest 키 , parse 앱이름, 디버그모드(true, false) ); // 기본환경설정
$Push->Send( 메시지, 제목 ); // 전체 발송
$Push->Send( 메시지, 제목, '', Y-m-d H:i 형태의 시간 ); // 예약 전체 발송
$Push->Send( 메시지, 제목, 디바이스코드(array or string) ); // 디바이스 지정 발송
$Push->Send( 메시지, 제목, 디바이스코드(array or string) , Y-m-d H:i 형태의 시간 ); // 디바이스 지정 예약발송
$Push->unsetConfig(); // 기본환경설정 초기화 (특이사항에서만 사용)
**/
class ParsePush {

    var $config;

    # parse의 api설정
    public function setConfig($AppID, $RestKey, $AppName, $Debug=false) {

        $this->config['app_id'] = $AppID;
        $this->config['rest_key'] = $RestKey;
        $this->config['app_name'] = $AppName;
        $this->config['debug'] = $Debug;
    }


    # 설정된 api 리셋한다.
    public function unsetConfig() {

        $this->config['auth'] = '';
        $this->config['app_code'] = '';
        $this->config['app_name'] = '';
        $this->config['debug'] = false;
    }


    # 설정을 확인한다.
    public function printConfig() {

        $config = $this->config;

        return $config;
    }


    # 메시지를 발송한다.
    public function Send($msg, $title='', $link='', $Device='', $SetTime='now') {


        // 초기값 선언
        $request_add = '';
        $notifications_add = '';

        // 통신 매쏘드 (고정) + url
        $method= 'push';
        $url = 'https://api.parse.com/1/' . $method;

        // 전송 설정
        if($SetTime == 'now') $SetTime = time();
        else $SetTime = strtotime($SetTime);

        // json 전송 포맷 설정
        if($Device) {

            $request = json_encode(array(
                            "where" => array(
                                "installationId" => $Device,
                                "appName" => $this->config['app_name'],
                            ),
                            "data" => array(
                                "alert" => $msg,
                                "uri" => $link,
                                "title" => $title
                            ),
                            "push_time" => $SetTime
                        ));
        }
        else {

            $request = json_encode(array(
                            "where" => array(
                                "appName" => $this->config['app_name'],
                            ),
                            "data" => array(
                                "alert" => $msg,
                                "uri" => $link,
                                "title" => $title
                            ),
                            "push_time" => $SetTime
                        ));
        }

        // 서버로 전송 및 결과 반환
        $rest = curl_init();
        curl_setopt($rest,CURLOPT_URL,$url);
        curl_setopt($rest,CURLOPT_PORT,443);
        curl_setopt($rest,CURLOPT_POST,1);
        curl_setopt($rest,CURLOPT_POSTFIELDS,$request);
        curl_setopt($rest,CURLOPT_HTTPHEADER,
                array("X-Parse-Application-Id: " . $this->config['app_id'] ,
                        "X-Parse-REST-API-Key: " . $this->config['rest_key'],
                        "Content-Type: application/json"));
        $Result['response'] = curl_exec($rest);
        $Result['info'] = curl_getinfo($rest);
        curl_close($rest);

        // 디버깅 모드시 결과 출력
        if ($this->config['debug'] === true) {

            print "[PW] request: $request<hr>";
            print "[PW] response: {$Result['response']}<hr>";
            print "[PW] info: " . print_r($Result['info'], true).'<hr>';
            print "[config] config: ". print_r($this->printConfig(), true).'<hr>';
        }
    }
}


# 기본설정
$ParsePush = new ParsePush;
$ParsePush->setConfig('디바이스아이디1', '디바이스아이디2', 'Terrorboy', false);


# 앱이름으로 전체 전송
$ParsePush->send('안녕? 난 내용~', '난 제목~!', 'http://example.com/');

# 다중전송
/*
$Device = array('디바이스아이디1', '디바이스아이디2');
foreach($Device as $k=>$v) {
    if(($k%30 == 0) && $k > 0) sleep(1);
    $ParsePush->send('title', '난 제목~!', 'http://example.com/', $v);
}
*/
첨부파일 parse_푸시테스트용.zip (1.9 MB)
 다음글 PushWoosh push 전송 클래스 (2)
 이전글 [10원짜리 팁] 크롬에서 올더게이트(PG) 결제 시 ... (2)

댓글

테러보이 / 2015/05/04 13:37:55 / 추천 0
생성자, 소멸자는 일부러 사용안했습니다.~
한대승(불의회상) / 2015/05/04 14:19:31 / 추천 0
좋은 정보 감사 합니다. ^^
제우스황제 / 2015/09/04 14:53:43 / 추천 0
최근에 parse 관련 내용을 보다보니 궁금한 게 있어서 여쭤봅니다. 

혹시 30req / s 라는 의미가 정확하게 어떤 의미 인가요?
1초당 30명에게 푸시가 된다는 의미 인가요? 

설정에서 30req / s로 제한한 경우 
초당 100 req를 날린다면 70req 는 어떻게 되나요?
에러, 또는 그냥 무시되는지 궁금합니다.
 
테러보이 / 2015/09/04 15:55:32 / 추천 0
@제우스황제

제가알기로 초당 처리가 30쿼리로 제한 한다는 내용으로 알고 있습니다.( 30 req / s == 30 request / 1 second )
저같은경우는 초당 30건이 넘을 경우 버퍼를 주어 초당 30씩 발송하도록 딜레이를 주었습니다.
또한, 전송 실패도 발송건으로 치는걸로 알고 있습니다.~
제우스황제 / 2015/09/08 21:44:01 / 추천 0
@테러보이 님 네 그렇군요 답변 감사합니다