Пример #1
0
 /**
  * 推送接口
  * @param  int     $push_type       1单个设备 2部分人(*常用)
  * @param  int     $device_type 设备类型 1:浏览器设备 2:pc设备 3:Android设备 4:ios设备 5:windows phone设备
  * @param  string  $title        标题(仅安卓)
  * @param  string  $content      留言正文
  * @param  int     $customtype   自定义类型,t
  * @param  string  $customvalue  自定义值,v
  * @param  string  $p_deviceToken 用户推送ID,百度里是buserid
  * @param  string  $tag_name     指定标签
  * @return array                 results
  */
 public static function pushMessage($push_type, $device_type, $title = '', $content, $customtype = null, $customvalue = null, $p_deviceToken = null, $tag_name = null)
 {
     $notification = new W2PUSH();
     $notification->setAppMasterSecret(W2PUSH::$SECRET_KEY);
     $params = $notification->getData();
     $params['appkey'] = W2PUSH::$API_KEY;
     $params['timestamp'] = strval(time());
     if ($device_type == 4) {
         $params['payload'] = array('aps' => array('alert' => $content));
         if (isset($customtype, $customvalue)) {
             $params['payload']['t'] = intval($customtype);
             $params['payload']['v'] = $customvalue;
         }
     } else {
         if ($device_type == 3) {
             if ($title == '') {
                 $title = $content;
             }
             $params['payload'] = array();
             $params['payload']['display_type'] = 'notification';
             $params['payload']['body'] = array();
             $params['payload']['body']['ticker'] = $title;
             $params['payload']['body']['title'] = $title;
             $params['payload']['body']['text'] = $content;
             $params['payload']['body']['after_open'] = 'go_app';
             if (isset($customtype, $customvalue)) {
                 $params['payload']['extra'] = array();
                 $params['payload']['extra']['t'] = intval($customtype);
                 $params['payload']['extra']['v'] = $customvalue;
             }
         } else {
             return Utility::getArrayForResults(RUNTIME_CODE_ERROR_PARAM, '请传入正确的设备类型,iOS 还是 安卓');
         }
     }
     $params['production_mode'] = static::$DEPLOY_STATUS == 2;
     //是否正式环境
     switch ($push_type) {
         case 1:
             $p_deviceToken = is_array($p_deviceToken) ? $p_deviceToken : explode(',', $p_deviceToken);
             if (count($p_deviceToken) == 0 || count($p_deviceToken) == 1 && $p_deviceToken[0] == null) {
                 return Utility::getArrayForResults(RUNTIME_CODE_ERROR_PARAM, '请传入正确的用户推送ID');
             } else {
                 if (count($p_deviceToken) < 500) {
                     if (count($p_deviceToken) > 1) {
                         $params['type'] = 'listcast';
                     } else {
                         $params['type'] = 'unicast';
                     }
                     $params['device_tokens'] = implode(',', $p_deviceToken);
                 } else {
                     $params['type'] = 'filecast';
                     $notification->uploadContents(implode("\n", $p_deviceToken));
                 }
             }
             break;
         case 2:
             $params['type'] = 'groupcast';
             $params['filter'] = array('where' => array('and' => array()));
             if ($tag_name != null) {
                 $tag_name = is_array($tag_name) ? $tag_name : explode(',', $tag_name);
                 foreach ($tag_name as $tag) {
                     $params['filter']['where']['and'][] = array('tag' => $tag);
                 }
             }
             break;
         default:
             return Utility::getArrayForResults(RUNTIME_CODE_ERROR_PARAM, 'push_type 1:单个人 2部分人 3所有人');
     }
     $notification->setData($params);
     try {
         $ret = $notification->send();
     } catch (Exception $e) {
         $ret = 'Caught exception: ' . $e->getMessage();
     }
     return Utility::getArrayForResults(RUNTIME_CODE_OK, '', array('push_type' => $push_type, 'messages' => $params['payload'], 'message_keys' => $params['timestamp'], 'optional' => $params, 'result' => $ret));
 }