1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189:
<?php
namespace LeanCloud;
/**
* Send Push notification to mobile devices
*/
class Push {
/**
* Notification data
*
* @var array
*/
private $data;
/**
* Notification options
*
* @var array
*/
private $options;
/**
* Initialize a Push notification
*
* @param array $data Notification data
* @param array $options Notification options
* @see self::setData, self::setOption
*/
public function __construct($data=array(), $options=array()) {
$this->data = $data;
$this->options = $options;
$this->options["prod"] = Client::$isProduction ? "prod": "dev";
}
/**
* Set notification data attributes
*
* Available attributes please see
* https://leancloud.cn/docs/push_guide.html#%E6%8E%A8%E9%80%81%E6%B6%88%E6%81%AF
*
* @param string $key Attribute key
* @param mixed $val Attribute value
* @return self
*/
public function setData($key, $val) {
$this->data[$key] = $val;
}
/**
* Set general option for notificiation
*
* Available options please see
* https://leancloud.cn/docs/push_guide.html#%E6%8E%A8%E9%80%81%E6%B6%88%E6%81%AF
*
* There are helper methods for setting most of options, use those
* if possible. Use this when no helper present, e.g. to enable
* "dev" environment in iOS:
*
* ```php
* $push->setOption("prod", "dev");
* ```
*
* @param string $key Option key
* @param mixed $val Option value
* @return self
* @see self::setWhere, self::setChannels, self::setPushTime ...
*/
public function setOption($key, $val) {
$this->options[$key] = $val;
return $this;
}
/**
* Set target channels
*
* @param array $channels List of channel names
* @return self
* @see self::setOption()
*/
public function setChannels($channels) {
return $this->setOption("channels", $channels);
}
/**
* Filter target devices by query
*
* The query must be over _Installation table.
*
* @param Query $query A query over _Installation
* @return self
* @see self::setOption()
*/
public function setWhere(Query $query) {
if ($query->getClassName() != "_Installation") {
throw new \RuntimeException("Query must be over " .
"_Installation table.");
}
return $this->setOption("where", $query);
}
/**
* Schedule a time to send message
*
* @param DateTime $time Time to send message to clients
* @return self
* @see self::setOption()
*/
public function setPushTime(\DateTime $time) {
return $this->setOption("push_time", $time);
}
/**
* Set expiration interval for message
*
* When client received message after the interval, it will not be
* displayed to user.
*
* @param int $interval Number of seconds (from now) to expire message
* @return self
* @see self::setOption()
*/
public function setExpirationInterval($interval) {
return $this->setOption("expiration_interval", $interval);
}
/**
* Set expiration time for message
*
* When client received message after the specified time, it will
* not be displayed to user.
*
* @param DateTime $time Time to expire message
* @return self
* @see self::setOption()
*/
public function setExpirationTime(\DateTime $time) {
return $this->setOption("expiration_time", $time);
}
/**
* Enable smooth push for message
*
* @param int $flowControl clients to push per second,
* a value <1000 is equivalent to 1000.
* @return self
* @see self::setOption()
*/
public function setFlowControl($flowControl) {
return $this->setOption("flow_control", $flowControl);
}
/**
* Encode to JSON representation
*
* @return array
*/
public function encode() {
$out = $this->options;
$out["data"] = $this->data;
$expire = isset($this->options["expiration_time"]) ? $this->options["expiration_time"] : null;
if (($expire instanceof \DateTime) ||
($expire instanceof \DateTimeImmutable)) {
$out["expiration_time"] = Client::formatDate($expire);
}
$pushTime = isset($this->options["push_time"]) ? $this->options["push_time"] : null;
if (($pushTime instanceof \DateTime) ||
($pushTime instanceof \DateTimeImmutable)){
$out["push_time"] = Client::formatDate($pushTime);
}
if (isset($this->options["where"])) {
$query = $this->options["where"]->encode();
$out["where"] = json_decode($query["where"], true);
}
return $out;
}
/**
* Send notification to LeanCloud
*
* @return array
*/
public function send() {
$out = $this->encode();
$resp = Client::post("/push", $out);
return $resp;
}
}