Skip to content

Commit

Permalink
Get url & body
Browse files Browse the repository at this point in the history
  • Loading branch information
sn01615 committed Apr 15, 2020
1 parent 726594b commit 1b75530
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 57 deletions.
43 changes: 2 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ composer require sn01615/umeng-php
```

customizedcast,通过alias进行推送, 对单个或者多个alias进行推送
2020年4月14日:测试有效
```php
$cc = (new UmengPush('***', '***'))
->setProductionMode(false)
// ->setGetUrlAndBody(true) // 直接返回url和body
->sendAndroidCustomizedcast([
'ticker' => '1', // 必填 通知栏提示文字
'title' => '2', // 必填 通知标题
Expand All @@ -25,47 +27,6 @@ $cc = (new UmengPush('***', '***'))
$aliasType = 'XXX');
```


单播(个人不推荐使用,因为token会变,推荐客户端给用户设置alias,使用alias+aliasType进行推送)

```php
use Umeng\UmengPush;

$key = "****";
$secret = "*****";
$push = new UmengPush($key, $secret);
$values = [
'ticker' => '1', // require 通知栏提示文字
'title' => '2', // require 通知标题
'text' => '3', // require 通知文字描述
'after_open' => 'go_app' // require 值可以为:
// "go_app": open app
// "go_url": 跳转到URL
// "go_activity": open activity
// "go_custom": 用户自定义内容。
];
echo $push->sendAndroidUnicast($values, [], 'device token');
# result: {"ret":"SUCCESS","data":{"msg_id":"uu77312149835056871500"}}
```
广播
```php
$key = "****";
$secret = "*****";
$push = new UmengPush($key, $secret);
$values = [
'ticker' => '1', // 必填 通知栏提示文字
'title' => '2', // 必填 通知标题
'text' => '3', // 必填 通知文字描述
'after_open' => 'go_app' // 必填 值可以为:
// "go_app": 打开应用
// "go_url": 跳转到URL
// "go_activity": 打开特定的activity
// "go_custom": 用户自定义内容。
];
echo $push->sendAndroidBroadcast($values, []);
# result: {"ret":"SUCCESS","data":{"task_id":"us41183149835484509400"}}
```

##### README
```
1.What's this?
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sn01615/umeng-php",
"description": "umeng push move to composer",
"type": "library",
"license": "GPL-3.0",
"license": "MIT",
"authors": [
{
"name": "YangLong",
Expand Down
61 changes: 47 additions & 14 deletions src/UmengPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class UmengPush
{

protected $appkey = NULL;
protected $appKey = NULL;

protected $appMasterSecret = NULL;

Expand All @@ -27,9 +27,17 @@ class UmengPush

private $production_mode = 'true';

private $getUrlAndBody = false;

/**
* UmengPush constructor.
* https://developer.umeng.com/docs/67966/detail/68343
* @param string $key AppKey 在Web后台的App应用信息页面获取
* @param string $secret App Master Secret 在Web后台的App应用信息页面获取
*/
function __construct($key, $secret)
{
$this->appkey = $key;
$this->appKey = $key;
$this->appMasterSecret = $secret;
$this->timestamp = strval(time());
}
Expand All @@ -51,7 +59,7 @@ function sendAndroidBroadcast(array $values, array $extra)
try {
$brocast = new AndroidBroadcast();
$brocast->setAppMasterSecret($this->appMasterSecret);
$brocast->setPredefinedKeyValue("appkey", $this->appkey);
$brocast->setPredefinedKeyValue("appkey", $this->appKey);
$brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
foreach ($_values as $key => $value) {
$brocast->setPredefinedKeyValue($key, $value);
Expand Down Expand Up @@ -111,7 +119,7 @@ public function sendAndroidUnicast(array $values, array $extra, $device_tokens)
try {
$unicast = new AndroidUnicast();
$unicast->setAppMasterSecret($this->appMasterSecret);
$unicast->setPredefinedKeyValue("appkey", $this->appkey);
$unicast->setPredefinedKeyValue("appkey", $this->appKey);
$unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
// Set your device tokens here
foreach ($_values as $key => $value) {
Expand Down Expand Up @@ -149,7 +157,7 @@ function sendAndroidFilecast(array $values, array $extra, array $tokens)
try {
$filecast = new AndroidFilecast();
$filecast->setAppMasterSecret($this->appMasterSecret);
$filecast->setPredefinedKeyValue("appkey", $this->appkey);
$filecast->setPredefinedKeyValue("appkey", $this->appKey);
$filecast->setPredefinedKeyValue("timestamp", $this->timestamp);
foreach ($values as $key => $value) {
$filecast->setPredefinedKeyValue($key, $value);
Expand Down Expand Up @@ -197,7 +205,7 @@ function sendAndroidGroupcast(array $values, array $extra, array $tags)
try {
$groupcast = new AndroidGroupcast();
$groupcast->setAppMasterSecret($this->appMasterSecret);
$groupcast->setPredefinedKeyValue("appkey", $this->appkey);
$groupcast->setPredefinedKeyValue("appkey", $this->appKey);
$groupcast->setPredefinedKeyValue("timestamp", $this->timestamp);
// Set the filter condition
$groupcast->setPredefinedKeyValue("filter", $filter);
Expand All @@ -218,6 +226,13 @@ function sendAndroidGroupcast(array $values, array $extra, array $tags)
}
}

/**
* @param array $values
* @param array $extra
* @param string $alias 别名,如:用户ID
* @param string $aliasType 别名类型,前端自定义设置,如:UID
* @return array|bool|string
*/
public function sendAndroidCustomizedcast(array $values, array $extra, $alias, $aliasType)
{
$values = array_merge([
Expand All @@ -235,7 +250,7 @@ public function sendAndroidCustomizedcast(array $values, array $extra, $alias, $
try {
$customizedcast = new AndroidCustomizedcast();
$customizedcast->setAppMasterSecret($this->appMasterSecret);
$customizedcast->setPredefinedKeyValue("appkey", $this->appkey);
$customizedcast->setPredefinedKeyValue("appkey", $this->appKey);
$customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
// Set your alias here, and use comma to split them if there are multiple alias.
// And if you have many alias, you can also upload a file containing these alias, then
Expand All @@ -250,13 +265,31 @@ public function sendAndroidCustomizedcast(array $values, array $extra, $alias, $
$customizedcast->setExtraField($key, $value);
}
// print("Sending customizedcast notification, please wait...\r\n");
return $customizedcast->send();
return $customizedcast->send($this->getGetUrlAndBody());
// print("Sent SUCCESS\r\n");
} catch (Exception $e) {
return "Caught exception: " . $e->getMessage();
}
}

/**
* @return bool
*/
public function getGetUrlAndBody()
{
return $this->getUrlAndBody;
}

/**
* @param bool $getUrlAndBody
* @return UmengPush
*/
public function setGetUrlAndBody($getUrlAndBody)
{
$this->getUrlAndBody = $getUrlAndBody;
return $this;
}

function sendAndroidCustomizedcastFileId(array $values, array $extra, array $tokens)
{
$values = [
Expand All @@ -274,7 +307,7 @@ function sendAndroidCustomizedcastFileId(array $values, array $extra, array $tok
try {
$customizedcast = new AndroidCustomizedcast();
$customizedcast->setAppMasterSecret($this->appMasterSecret);
$customizedcast->setPredefinedKeyValue("appkey", $this->appkey);
$customizedcast->setPredefinedKeyValue("appkey", $this->appKey);
$customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
// if you have many alias, you can also upload a file containing these alias, then
// use file_id to send customized notification.
Expand All @@ -300,7 +333,7 @@ function sendIOSBroadcast()
try {
$brocast = new IOSBroadcast();
$brocast->setAppMasterSecret($this->appMasterSecret);
$brocast->setPredefinedKeyValue("appkey", $this->appkey);
$brocast->setPredefinedKeyValue("appkey", $this->appKey);
$brocast->setPredefinedKeyValue("timestamp", $this->timestamp);

$brocast->setPredefinedKeyValue("alert", "IOS 骞挎挱娴嬭瘯");
Expand All @@ -327,7 +360,7 @@ public function sendIOSUnicast(array $values, array $extra, $device_tokens)
try {
$unicast = new IOSUnicast();
$unicast->setAppMasterSecret($this->appMasterSecret);
$unicast->setPredefinedKeyValue("appkey", $this->appkey);
$unicast->setPredefinedKeyValue("appkey", $this->appKey);
$unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
// Set your device tokens here
$unicast->setPredefinedKeyValue("device_tokens", $device_tokens);
Expand Down Expand Up @@ -355,7 +388,7 @@ function sendIOSFilecast()
try {
$filecast = new IOSFilecast();
$filecast->setAppMasterSecret($this->appMasterSecret);
$filecast->setPredefinedKeyValue("appkey", $this->appkey);
$filecast->setPredefinedKeyValue("appkey", $this->appKey);
$filecast->setPredefinedKeyValue("timestamp", $this->timestamp);

$filecast->setPredefinedKeyValue("alert", "IOS 鏂囦欢鎾祴璇�");
Expand Down Expand Up @@ -399,7 +432,7 @@ function sendIOSGroupcast()

$groupcast = new IOSGroupcast();
$groupcast->setAppMasterSecret($this->appMasterSecret);
$groupcast->setPredefinedKeyValue("appkey", $this->appkey);
$groupcast->setPredefinedKeyValue("appkey", $this->appKey);
$groupcast->setPredefinedKeyValue("timestamp", $this->timestamp);
// Set the filter condition
$groupcast->setPredefinedKeyValue("filter", $filter);
Expand All @@ -421,7 +454,7 @@ function sendIOSCustomizedcast()
try {
$customizedcast = new IOSCustomizedcast();
$customizedcast->setAppMasterSecret($this->appMasterSecret);
$customizedcast->setPredefinedKeyValue("appkey", $this->appkey);
$customizedcast->setPredefinedKeyValue("appkey", $this->appKey);
$customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);

// Set your alias here, and use comma to split them if there are multiple alias.
Expand Down
2 changes: 1 addition & 1 deletion src/UmengPush/UmengNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function setAppMasterSecret($secret)

abstract function setPredefinedKeyValue($key, $value);

function send($getUrlAndBody = null)
function send($getUrlAndBody = false)
{
// check the fields to make sure that they are not NULL
$this->isComplete();
Expand Down

0 comments on commit 1b75530

Please sign in to comment.