/** * 以keyvalue的组合方式来重设tag,(先删除已有的keyvalue) * @param string $p_deviceTokens 用户推送ID,百度里是buserid,支持数组或逗号隔开的字符串 * @param int $device_type 设备类型 1:浏览器设备 2:pc设备 3:Android设备 4:ios设备 5:windows phone设备 * @param string $tag_key key * @param string $tag_values value值,若为空,则删除所有key数据 */ public static function BatchSetTagValue($p_deviceTokens, $device_type, $tag_key, $tag_values) { $newTagnames = array(); $addTagnames = array(); $delTagnames = array(); $ret = array(); $tag_values = is_array($tag_values) ? $tag_values : explode(',', $tag_values); foreach ($tag_values as $tag_value) { if ($tag_value != null) { $newTagnames[] = $tag_key . $tag_value; } } $p_deviceTokens = is_array($p_deviceTokens) ? $p_deviceTokens : explode(',', $p_deviceTokens); foreach ($p_deviceTokens as $p_deviceToken) { $tagnames = W2PUSH::queryTokenTags($p_deviceToken, $device_type); if (is_array($tagnames)) { $addTagnames = array_diff($newTagnames, $tagnames); foreach ($tagnames as $tagname) { if (strpos($tagname, $tag_key) === 0 && !in_array($tagname, $newTagnames)) { $delTagnames[] = $tagname; } } } $ret[] = W2PUSH::BatchAddTag($p_deviceToken, $device_type, $addTagnames); $ret[] = W2PUSH::BatchDelTag($p_deviceToken, $device_type, $delTagnames); } return $ret; }
/** * 设定用户的版本号 * @param string $p_buserid 用户推送ID,百度里是buserid * @param string $p_cilentVersion 版本号 */ public static function setTagWithCilentVersion($p_buserid, $p_cilentVersion = null) { return W2PUSH::setTagWithPrefix($p_buserid, $p_cilentVersion, 'v'); }
/** * 推送接口 * @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)); }
/** * 设置设备和用户绑定,以及设置对应tag * @param string $p_deviceToken token * @param UserModel $p_userModel 用户对象,或留空 */ public static function setDeviceWithUser($p_deviceToken, $p_userModel = null) { if ($p_deviceToken != null) { $tmpModel = DeviceHandler::loadModelFirstInList(array('deviceToken' => $p_deviceToken)); if (is_object($tmpModel)) { if (is_object($p_userModel)) { $tmpModel->setUserId($p_userModel->getId()); W2PUSH::BatchSetTagValue($tmpModel->getDeviceToken(), $tmpModel->getDeviceType(), 'language', $p_userModel->getLanguage()); } else { $tmpModel->setUserId(0); W2PUSH::BatchSetTagValue($tmpModel->getDeviceToken(), $tmpModel->getDeviceType(), 'language', null); } DeviceHandler::saveModel($tmpModel); } } }