/** * 推送接口 * @param int $push_type 1单个设备 2部分人(*常用) 3所有人 * @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_buserid 用户推送ID,百度里是buserid * @param string $tag_name 指定标签 * @return array results */ public static function pushMessage($push_type, $device_type, $title = '', $content, $customtype = null, $customvalue = null, $p_buserid = null, $tag_name = null) { $message_keys = uniqid(); //函数基于以微秒计的当前时间,生成一个唯一的 ID。 $channel = new Channel(W2PUSH::$API_KEY, W2PUSH::$SECRET_KEY); if (isset($tag_name)) { $optional[Channel::TAG_NAME] = $tag_name; } $optional[Channel::MESSAGE_TYPE] = 1; //通知 $optional[Channel::DEPLOY_STATUS] = static::$DEPLOY_STATUS; //1 测试状态 2生产状态 switch ($push_type) { case 1: if ($p_buserid === null) { return Utility::getArrayForResults(RUNTIME_CODE_ERROR_PARAM, '请传入正确的用户推送ID'); } $optional[Channel::USER_ID] = $p_buserid; case 2: $optional[Channel::DEVICE_TYPE] = $device_type; if ($device_type == 4) { $messages = '{' . '"aps":{' . '"alert":"' . $content . '",' . '"sound":"",' . '"badge":1' . '}'; if (isset($customtype, $customvalue)) { $messages .= ',"t":' . intval($customtype) . ',"v":"' . $customvalue . '"'; } $messages .= '}'; } else { if ($device_type == 3) { $messages = '{' . '"title": "' . $title . '",' . '"description": "' . $content . '",' . '"notification_builder_id": 0,' . '"notification_basic_style": 7,' . '"open_type": 2,' . '"net_support" : 1,' . '"pkg_content" : ",' . '"custom_content": {"t":' . intval($customtype) . ',"v":"' . $customvalue . '"}' . '}'; } else { return Utility::getArrayForResults(RUNTIME_CODE_ERROR_PARAM, '请传入正确的设备类型,iOS 还是 安卓'); } } $ret = $channel->pushMessage($push_type, $messages, $message_keys, $optional); break; case 3: $ret = $channel->pushMessage($push_type, $messages, $message_keys); break; default: return Utility::getArrayForResults(RUNTIME_CODE_ERROR_PARAM, 'push_type 1:单个人 2部分人 3所有人'); } if (false === $ret) { $ret = array(); $ret[] = 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!!'; $ret[] = 'ERROR NUMBER: ' . $channel->errno(); $ret[] = 'ERROR MESSAGE: ' . $channel->errmsg(); $ret[] = 'REQUEST ID: ' . $channel->getRequestId(); } return Utility::getArrayForResults(RUNTIME_CODE_OK, '', array('push_type' => $push_type, 'messages' => $messages, 'message_keys' => $message_keys, 'optional' => $optional, 'result' => $ret)); }
public function push($data) { require_once DIR_SYSTEM . '/baidu/Channel.class.php'; // var_dump(BAE_API_KEY); $channel = new Channel(BAE_API_KEY, BAE_SECRET_KEY); if (array_key_exists('description', $data)) { if (mb_strlen($data['description'], 'utf-8') > 39) { $data['description'] = mb_substr($data['description'], 0, 37, 'utf-8') . '...'; } } //推送类型,1单人(必须指定user_id和channel_id)、2群组(必须指定tag_name)、3所有人 $push_type = $data['push_type']; $message["title"] = array_key_exists('title', $data) ? $data['title'] : ''; //android必选 $message["description"] = $data['description']; //android必选 // $message["custom_content"] = array ("jump_id" => array_key_exists('jump_id', $data) ? $data['jump_id'] : '', "url" => array_key_exists('url', $data) ? $data['url'] : '');//android自定义字段 $message["aps"] = array("alert" => $data['description'], "sound" => "", "badge" => 1); //ios特有字段 if (array_key_exists('custom_content', $data)) { if ($data['device_type'] == 3) { //android $message["custom_content"] = $data['custom_content']; } elseif ($data['device_type'] == 4) { //ios foreach ($data['custom_content'] as $key => $value) { $message[$key] = $value; } } } $message_key = "msg_key"; //不支持ios //1:浏览器设备、2:pc设备、3:Android设备、4:ios设备、5:windows phone设备 $optional[Channel::DEVICE_TYPE] = $data['device_type']; //Channel::USER_ID,标识用户的ID,分为百度账号与无账号体系:无账号体系的user_id根据端上属性生成;百度账号体系根据账号生成user_id。 $optional[Channel::USER_ID] = array_key_exists('user_id', $data) ? $data['user_id'] : ''; //Channel::MESSAGE_TYPE,消息类型,0:消息(透传),1:通知 $optional[Channel::MESSAGE_TYPE] = array_key_exists('message_type', $data) ? $data['message_type'] : 1; //ios部署状态,1开发状态,默认是2生产状态 $optional[Channel::DEPLOY_STATUS] = array_key_exists('deploy_status', $data) ? $data['deploy_status'] : ''; $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); if (false === $ret) { // $err = 'ERROR NUMBER: ' . $channel->errno() . '; ERROR MESSAGE: ' . $channel->errmsg() . '; REQUEST ID: ' . // $channel->getRequestId() . ";"; // Log::record($err); // var_dump( array ("ret" => SYS_RET_ERR, "err" => $err) ); } else { // var_dump( array ("ret" => 0, "err" => "") ); } }
private function test_pushMessage_ios($user_id, $body) { $channel = new Channel($this->apiKey, $this->secretKey); $push_type = 1; //推送单播消息 $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user //指定发到ios设备 $optional[Channel::DEVICE_TYPE] = 4; //指定消息类型为通知 $optional[Channel::MESSAGE_TYPE] = 1; //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2. //旧版本曾采用不同的域名区分部署状态,仍然支持。 $optional[Channel::DEPLOY_STATUS] = 2; //通知类型的内容必须按指定内容发送,示例如下: // $message = '{ // "aps":{ // "alert":"msg from baidu push", // "sound":"", // "badge":0 // } // }'; $message_key = "msg_key"; $ret = $channel->pushMessage($push_type, $body, $message_key, $optional); file_put_contents("/tmp/test.log", '########$bodyaps=' . $body . '#######' . "\n", FILE_APPEND); file_put_contents("/tmp/test.log", '########$ret=' . json_encode($ret) . '#######' . "\n", FILE_APPEND); if (false === $ret) { file_put_contents("/tmp/test.log", '########WRONG, ' . __FUNCTION__ . ' ERROR!!!!!#######' . "\n", FILE_APPEND); file_put_contents("/tmp/test.log", '########ERROR NUMBER: ' . $channel->errno() . '#######' . "\n", FILE_APPEND); file_put_contents("/tmp/test.log", '########ERROR MESSAGE: ' . $channel->errmsg() . '#######' . "\n", FILE_APPEND); file_put_contents("/tmp/test.log", '########REQUEST ID: ' . $channel->getRequestId() . '#######' . "\n", FILE_APPEND); return false; } else { //right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ; //right_output ( 'result: ' . print_r ( $ret, true ) ) ; return TRUE; } }
function test_pushMessage_ios($user_id) { global $apiKey; global $secretKey; $channel = new Channel($apiKey, $secretKey); $push_type = 1; //推送单播消息 $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user //指定发到ios设备 $optional[Channel::DEVICE_TYPE] = 4; //指定消息类型为通知 $optional[Channel::MESSAGE_TYPE] = 1; //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2. //旧版本曾采用不同的域名区分部署状态,仍然支持。 $optional[Channel::DEPLOY_STATUS] = 1; //通知类型的内容必须按指定内容发送,示例如下: $message = '{ "aps":{ "alert":"msg from baidu push", "sound":"", "badge":0 } }'; $message_key = "msg_key"; $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); if (false === $ret) { error_output('WRONG, ' . __FUNCTION__ . ' ERROR!!!!!'); error_output('ERROR NUMBER: ' . $channel->errno()); error_output('ERROR MESSAGE: ' . $channel->errmsg()); error_output('REQUEST ID: ' . $channel->getRequestId()); } else { right_output('SUCC, ' . __FUNCTION__ . ' OK!!!!!'); right_output('result: ' . print_r($ret, true)); } }
function pushMessage_ios ($user_id,$MsgContent,$custom_content){ $custom_content = substr($custom_content,1,-1); $apiKey = C('baidu_apiKey'); $secretKey = C("baidu_secretKey"); $channel = new Channel ( $apiKey, $secretKey ) ; $push_type = 1; //推送单播消息 $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user //指定发到ios设备 $optional[Channel::DEVICE_TYPE] = 4; //指定消息类型为通知 $optional[Channel::MESSAGE_TYPE] = $_POST['MESSAGE_TYPE']; //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2. //旧版本曾采用不同的域名区分部署状态,仍然支持。 $optional[Channel::DEPLOY_STATUS] = 2; //通知类型的内容必须按指定内容发送,示例如下: $message = '{ "title": "最新消息", "description": "'.$MsgContent.'", "aps":{ "alert":"'.$MsgContent.'", "sound":"", "badge":0 }, '.$custom_content.' }'; $message_key = "msg_key"; $ret = $channel->pushMessage ( $push_type, $message, $message_key, $optional ) ; if ( false === $ret ){ return 0; } else{ return 1; } }
<?php /** * 百度云推送 * @author 晨曦 * @Email jakehu1991@163.com * @Website http://www.jakehu.me/ * @version 20130706 */ require_once "./Channel.class.php"; $apiKey = "132132132132132131232"; $secretKey = "132132132132132131232"; $channel = new Channel($apiKey, $secretKey); $push_type = 3; //推送广播消息 $optional[Channel::MESSAGE_TYPE] = 1; $message = "{ \r\n 'title': '132132132132132131232',\r\n 'description': '132132132132132131232', \r\n }"; $message_key = "msg_key"; $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); print_r($ret);
/** * @param $user_id 用户标识 * @param $pType 推送类型: * 推送消息到某个user,设置push_type = 1; * 推送消息到一个tag中的全部user,设置push_type = 2; * 推送消息到该app中的全部user,设置push_type = 3; * @param $message 指定消息内容 * @param $device_type 设备类型:Android:3;Ios:4 */ function push_message_customer($user_id, $pType, $message, $device_type, $msg_key) { // header("Content-Type:text/html; charset=utf-8"); $log = new Log(); import('@.ORG.Channel'); global $api_key; global $secret_key; $api_key = "V7yX7yY8tbvcsBchBSyTDc8n"; $secret_key = "IbsBMY4ebgPlyayj3o2m4ZhKxqu18fb8"; $channel = new Channel($api_key, $secret_key); $push_type = $pType; $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user //optional[Channel::TAG_NAME] = "xxxx"; //如果推送tag消息,需要指定tag_name //指定发到哪种设备 $optional[Channel::DEVICE_TYPE] = $device_type; //指定消息类型为通知 if ($device_type == 3) { $optional[Channel::MESSAGE_TYPE] = 0; //0:消息; $log->write('向 android 发送消息'); } else { if ($device_type == 4) { //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2. //旧版本曾采用不同的域名区分部署状态,仍然支持。 $optional[Channel::DEPLOY_STATUS] = 1; $optional[Channel::MESSAGE_TYPE] = 1; //1:通知; $log->write('向 ios 发送通知'); } } //通知类型的内容必须按指定内容发送,示例如下: $message_key = $msg_key; $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); if (false === $ret) { $log->write('WRONG, ' . __FUNCTION__ . ' ERROR!!!!<br/>'); $log->write('ERROR NUMBER: ' . $channel->errno() . '<br/>'); // $log->write ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) . '<br/>' ); // $log->write ( 'REQUEST ID: ' . $channel->getRequestId ( ) . '<br/>' ); return false; } else { $log->write('SUCC, ' . __FUNCTION__ . ' OK!!!!!' . '<br/>'); // $log->write ( 'result: ' . print_r ( $ret, true ) . '<br/>' ); return true; } }
/** * 推送ios设备消息 * @param type $paramArr */ public static function pushMessageIos($paramArr) { $options = array('apiKey' => '', 'secretKey' => '', 'alert' => '', 'pushType' => '', 'userId' => '', 'tagName' => '', 'channelId' => '', 'messageType' => ''); if (is_array($paramArr)) { $options = array_merge($options, $paramArr); } extract($options); $channel = new Channel($apiKey, $secretKey); $push_type = $pushType; //推送单播消息 //如果推送单播消息,需要指定user if ($userId) { $optional[Channel::USER_ID] = $userId; } //如果推送tag消息,需要指定tag_name if ($tagName) { $optional[Channel::TAG_NAME] = $tagName; } //指定发到ios设备 $optional[Channel::DEVICE_TYPE] = 4; //指定消息类型为通知 $optional[Channel::MESSAGE_TYPE] = $messageType; //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2. //旧版本曾采用不同的域名区分部署状态,仍然支持。 $optional[Channel::DEPLOY_STATUS] = 1; //通知类型的内容必须按指定内容发送,示例如下: $message = '{ "aps":{ "alert":"' . ZOL_String::convToU8($alert) . '", "sound":"", "badge":0 } }'; $message_key = "msg_key"; $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); return $ret; }
function test_pushMessage_ios($user_id) { global $apiKey; global $secretKey; $channel = new Channel($apiKey, $secretKey); //注意百度push服务对ios dev版与ios release版采用不同的域名. //如果是dev版请修改push服务器域名"https://channel.iospush.api.duapp.com", release版则使用默认域名,修改域名使用setHost接口 //$channel->setHost("https://channel.iospush.api.duapp.com"); $push_type = 1; //推送单播消息 $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user //指定发到ios设备 $optional[Channel::DEVICE_TYPE] = 4; //指定消息类型为通知 $optional[Channel::MESSAGE_TYPE] = 1; //通知类型的内容必须按指定内容发送,示例如下: $message = '{ "aps":{ "alert":"msg from baidu push", "Sound":"", "Badge":0 } }'; $message_key = "msg_key"; $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); if (false === $ret) { error_output('WRONG, ' . __FUNCTION__ . ' ERROR!!!!!'); error_output('ERROR NUMBER: ' . $channel->errno()); error_output('ERROR MESSAGE: ' . $channel->errmsg()); error_output('REQUEST ID: ' . $channel->getRequestId()); } else { right_output('SUCC, ' . __FUNCTION__ . ' OK!!!!!'); right_output('result: ' . print_r($ret, true)); } }
static function pushBaidu($notifyMsg) { if ($notifyMsg->type === NotificationsManager::DEVICE_ISO) { $channel = NotificationsManager::initBaiduCertIOS($notifyMsg); } else { $channel = new Channel($notifyMsg->appid, $notifyMsg->restkey); } $message_key = md5(date('Y-m-d-H-M-S', time())); $optional[Channel::MESSAGE_TYPE] = 1; // if(isset($notifyMsg->channels) && is_array($notifyMsg->channels) && count($notifyMsg->channels)>0){ // $push_type = 2; // }else { // $push_type = 3; //推送广播消息 // } $push_type = 3; $message = array(); if ($notifyMsg->type === NotificationsManager::DEVICE_ISO) { $optional[Channel::DEVICE_TYPE] = 4; $apps = array(); if (isset($notifyMsg->title) && !is_null($notifyMsg->title)) { $message['title'] = $notifyMsg->title; } $optional[Channel::DEVICE_TYPE] = 4; if (isset($notifyMsg->prod_id) && !is_null($notifyMsg->prod_id)) { $message['prod_id'] = $notifyMsg->prod_id; } if (isset($notifyMsg->prod_type) && !is_null($notifyMsg->prod_type)) { $message['prod_type'] = $notifyMsg->prod_type; } if (isset($notifyMsg->push_type) && !is_null($notifyMsg->push_type)) { $message['push_type'] = $notifyMsg->push_type; } if (isset($notifyMsg->alert) && !is_null($notifyMsg->alert)) { $apps['alert'] = $notifyMsg->alert; } $apps['Sound'] = ''; $apps['Badge'] = 0; $message['aps'] = $apps; } else { if ($notifyMsg->type === NotificationsManager::DEVICE_ANDROID) { $custom_content = array(); if (isset($notifyMsg->title) && !is_null($notifyMsg->title)) { $message['title'] = $notifyMsg->title; } $optional[Channel::DEVICE_TYPE] = 3; if (isset($notifyMsg->prod_id) && !is_null($notifyMsg->prod_id)) { $custom_content['prod_id'] = $notifyMsg->prod_id; } if (isset($notifyMsg->prod_type) && !is_null($notifyMsg->prod_type)) { $custom_content['prod_type'] = $notifyMsg->prod_type; } if (isset($notifyMsg->push_type) && !is_null($notifyMsg->push_type)) { $custom_content['push_type'] = $notifyMsg->push_type; } if (isset($notifyMsg->alert) && !is_null($notifyMsg->alert)) { $message['description'] = $notifyMsg->alert; } $message['custom_content'] = $custom_content; } } // var_dump($message); var_dump($optional); $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); if (false === $ret) { return array('code' => '201', 'response' => $channel->errmsg()); } else { return array('code' => '200', 'response' => $ret); } }
public function PushMessageToDaikeTag($tag = 'diancanyodaike', $title = "订单处理延迟,请跟进", $content = '如题,请及时处理') { require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "baidusdk/Channel.class.php"; $baidukey = (include Wind::getRealPath('ROOT:conf.baidudaikekey.php', true)); $apiKey = $baidukey['appid']; $secretKey = $baidukey['appsecret']; $env = $baidukey['env']; $title = $env . $title; $channel = new Channel($apiKey, $secretKey); //推送消息到某个user,设置push_type = 1; //推送消息到一个tag中的全部user,设置push_type = 2; // $push_type = 2; //$optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user $optional[Channel::TAG_NAME] = $tag; //如果推送tag消息,需要指定tag_name //指定发到android设备 $optional[Channel::DEVICE_TYPE] = 3; //指定消息类型为通知 $optional[Channel::MESSAGE_TYPE] = 1; //通知类型的内容必须按指定内容发送,示例如下: $message = '{ "title": "##title##", "description": "##content##", "notification_basic_style":7, "open_type":1, "url":"http://www.baidu.com" }'; $message = str_replace('##title##', $title, $message); $message = str_replace('##content##', $content, $message); $message_key = "msg_key"; try { $ret = $channel->pushMessage($push_type, $message, $message_key, $optional); if (false === $ret) { var_dump($channel); } else { } } catch (Exception $ex) { } }