123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- //todo 确认文件是否删除
- /**
- * Created by PhpStorm.
- * User: dell
- * Date: 2016/4/15
- * Time: 16:27
- */
- // 发送类型, android 或者 ios
- class platform_type extends SplEnum
- {
- const __default = self::unicast;
- const android = 1;
- const ios = 2;
- }
- // 消息类型
- class msg_type extends SplEnum
- {
- const __default = self::unicast;
- const unicast = "unicast";
- const listcast = "listcast";
- const filecast = "filecast";
- const broadcast = "broadcast";
- const groupcast = "groupcast";
- const customizedcast = "customizedcast";
- const alias = "alias";
- const file_id = "file_id";
- }
- class status_code extends SplEnum
- {
- const __default = self::OK;
- const OK = 200;
- const CREATED = 201;
- const ACCEPTED = 202;
- const BAD_REQUEST = 400;
- const UNAUTHORIZED = 401;
- const FORBIDDEN = 403;
- const NOT_FOUND = 404;
- const INTERNAL_SERVICE_ERROR = 500;
- }
- class display_type extends SplEnum
- {
- const __default = self::notification;
- const notification = "notification";
- const message = "message";
- }
- /**
- * Class upush
- *
- * 友盟推送内容
- */
- class upush
- {
- const PUSH_URL = "http://msg.umeng.com/api/send";
- const METHOD = "POST";
- const APP_MASTER_SECRET = "r6w2a8z9x8zonh7qmk8ds2fvypu02wpj";
- const APPKEY = "5631efd4e0f55a8770000027";
- private $platform;
- private $type;
- private $alias_type;
- private $alias;
- // 个人信息
- private $member_id = array(); // 必填选项
- private $devices_token = array();
- // 通知展示内容
- private $ticker;
- private $title;
- private $text;
- private $icon;
- private $largeIcon;
- private $img;
- private $sound;
- public function __construct()
- {
- $platform = platform_type::android;
- $type = msg_type::unicast;
- $ticker = "ticker";
- $title = "title";
- $text = "text";
- }
- // 设置属性
- private function __set($property_name, $value)
- {
- $this->$property_name = $value;
- }
- // 获取属性
- private function __get($property_name)
- {
- if (isset($this->$property_name)) {
- return ($this->$property_name);
- } else {
- return (NULL);
- }
- }
- /**
- * post发送数据
- *
- * @param $url
- * @param $data_string
- * @return array
- */
- public function http_post_data($url, $data_string)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($data_string))
- );
- ob_start();
- curl_exec($ch);
- $return_content = ob_get_contents();
- ob_end_clean();
- $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- return array($return_code, $return_content);
- }
- /**
- * 生成签名
- *
- * @param $http_method
- * @param $url
- * @param $post_body
- * @param $app_master_secret
- * @return string
- */
- private function make_sign($http_method, $url, $post_body, $app_master_secret)
- {
- return strtolower(md5($http_method . $url . json_encode($post_body) . $app_master_secret));
- }
- // android 打包
- private function android_pack()
- {
- $pack = array();
- $pack['appkey'] = self::APPKEY;
- $pack['timestamp'] = time();
- $pack['type'] = msg_type::unicast;
- $pack['device_tokens'] = "Aj9AympPsCha5zmPhrV0DbnOZJYF0pqKY5jdKvFy_Hbu";
- // $pack['device_tokens'] = $this->devices_token;
- $pack['alias_type'] = "";
- $pack['alias'] = "";
- $pack['file_id'] = "";
- $pack['filter'] = "";
- $payload = array();
- $body = array();
- $body['ticker'] = $this->ticker;
- $body['title'] = $this->title;
- $body['text'] = $this->text;
- $body['icon'] = $this->icon;
- $body['largeIcon'] = $this->largeIcon;
- $body['img'] = $this->img;
- $body['sound'] = $this->sound;
- $body['builder_id'] = "";
- $body['play_vibrate'] = "true";
- $body['play_lights'] = "true";
- $body['play_sound'] = "true";
- $body['after_open'] = "go_app";
- $body['url'] = "";
- $body['activity'] = "";
- $body['custom'] = "";
- $extra = array();
- $extra['key1'] = "key1";
- $extra['key1'] = "key2";
- $extra['key1'] = "key3";
- $payload['display_type'] = display_type::message;
- $payload['body'] = $body;
- $payload['extra'] = $extra;
- $pack['payload'] = $payload;
- $policy = array();
- $policy['start_time'] = "";
- $policy['expire_time'] = "";
- $policy['max_send_num'] = "100";
- $policy['out_biz_no'] = "10001";
- // $pack['policy'] = $policy;
- $pack['production_mode'] = "false";
- $pack['description'] = "";
- $pack['thirdparty_id'] = "";
- return $pack;
- }
- private function pre_pack()
- {
- if (!empty($this->member_id)) {
- $result = Model()->table('device_binding')->where(array('member_id' => array('in', $this->member_id)))->select();
- foreach ($result as $value) {
- array_push($this->devices_token, $value['upush_device_token']);
- }
- return true;
- } else {
- return false;
- }
- }
- // android 推送
- public function android_push()
- {
- // $ret = $this->pre_pack();
- // if ($ret) {
- $pack_body = self::android_pack();
- $url = self::PUSH_URL . '?sign=' . self::make_sign("POST", self::PUSH_URL, $pack_body, self::APP_MASTER_SECRET);
- return $this->http_post_data($url, json_encode($pack_body));
- // } else {
- // return array(600, '参数输入不完整');
- // }
- }
- }
|