最近,app要做推送功能,就查阅了一些资料,找到一段示例代码:
拼接sings规则在文档中分别标注的有安卓和iOS方法
<?php
namespace Home\Controller;
use Think\Controller;
use Think\Upload;
class PushController extends CommentController
{
public function push()
{
// Set your appkey and master secret here
$demo = new Demo("device_token", "master_server");
//$demo->sendAndroidBroadcast();
// $demo->sendAndroidUnicast();
// $demo->sendIOSBroadcast();
/* these methods are all available, just fill in some fields and do the test
* $demo->sendAndroidBroadcast();
* $demo->sendAndroidFilecast();
* $demo->sendAndroidGroupcast();
* $demo->sendAndroidCustomizedcast();
* $demo->sendAndroidCustomizedcastFileId();
*
* $demo->sendIOSBroadcast();
* $demo->sendIOSUnicast();
* $demo->sendIOSFilecast();
* $demo->sendIOSGroupcast();
* $demo->sendIOSCustomizedcast();
*
*/
}
/*
推送测试
*/
private $_config = array();
public function __construct() {
parent::__construct();
$this->_config = $this->_makeConfig();
}
/*
*安卓发送
*/
//安卓测试接口
public function testA() {
$device_tokens = array('An7dDiMH66cDSHEn-bL5YvnBNOn0_24235354dvd');//安卓device_token
$this->PtoAndroid($device_tokens,'通知栏的文字','测试标题','测试内容');
//$this->PtoAndroid('通知栏的文字','测试标题','测试内容');
}
//安卓发送请求
public function PtoAndroid($device_tokens,$ticker,$title,$text) {
if(count($device_tokens) > 500) {
die('设备超过500个');
}
//拼接签名
$post_data = $this->_android($device_tokens,$ticker,$title,$text);
$sign = $this->_makeSign($post_data,1);
$url = $this->_config['url'].'?sign='.$sign;
//发送请求
$res = $this->_curl($url,$post_data);
//判断
if($res['ret'] != 'SUCCESS'){
//发送失败,
die($res['data']->error_code);
}else{
die($res['data']->msg_id);
}
}
/*
android post_body设置
@param ticker str 提示栏文字
title str 通知标题
text str 文字描述
device_tokens array 设备号
"after_open": "xx" // 必填 值可以为: 点击"通知"的后续行为,默认为打开app。
"go_app": 打开应用
"go_url": 跳转到URL
"go_activity": 打开特定的activity
"go_custom": 用户自定义内容。
@return
*/
private function _android($device_tokens,$ticker,$title,$text) {
$temp_arr = array(
'appkey' => $this->_config['and_app_key'],
'timestamp' => time(),
'type' => 'listcast', //listcast-列播(要求不超过500个device_token)
'device_tokens' => trim(implode($device_tokens),','),
'payload' => array(
'display_type' => 'notification',//通知,友盟接管处理,消息类型:notification-通知,message-消息
'body' => array(
'ticker' => $ticker, //通知栏提示文字
'title' => $title,
'text' => $text,
'after_open' => 'go_custom',
'custom' => array(['value' =>'do things']), //点击通知后做的事
),
),
'description' => "通知用户",//描述
'production_mode' => 'false',//可选 正式/测试模式。测试模式下,只会将消息发给测试设备。
);
return json_encode($temp_arr);
}
/*
iOS发送
*/
//ios测试
public function testIos($device_token,$text) {
$device_tokens = array("$device_token");//ios device_token
$this->PtoIos($device_tokens,$text);
}
public function PtoIos($device_tokens,$text) {
// if(count($device_tokens) > 500) {
// die('设备超过500个');
// }
//拼接post数据
$post_data = $this->_ios($device_tokens,$text);
//拼接签名
$sign = $this->_makeSign($post_data,2);
$url = $this->_config['url'] .'?sign='.$sign;
//发送请求
$res = $this->_curl($url,$post_data);
//判断
if($res['ret'] != 'SUCCESS') {
die($res['data']->error_code);
//发送失败,
}else{
die($res['data']->msg_id);
}
}
//生成配置文件,安卓,ios appkey和app_master_secret
private function _makeConfig() {
return array(
'method' => 'POST',
'url' => 'http://msg.umeng.com/api/send',
'ios_app_key' => '59facd1b734be414ce00453d2',
'ios_app_master_secret' => 'ggvgx9ynzdhhswqj8ee8kpsasjd343mf',
'and_app_key' => '59fac8d8734be4079a000478',
'and_app_master_secret' => 'tpbqo8sgxsrlt32ugsaovdntitsfgdg6g',
);
}
/*
生成签名
@param post_body 请求体
type 1,安卓,2 ios
@return str
*/
private function _makeSign($post_body,$type) {
if($type == 1) {
return strtolower(md5($this->_config['method'].$this->_config['url'].$post_body.$this->_config['and_app_master_secret']));
}else {
return strtolower(md5($this->_config['method'].$this->_config['url'].$post_body.$this->_config['ios_app_master_secret']));
}
}
/*
ios post_body消息体设置
@param ticker str 提示栏文字
title str 通知标题
text str 文字描述
device_tokens array 设备号
@return
*/
private function _ios($device_tokens,$text) {
$temp_arr = array(
'appkey' => $this->_config['ios_app_key'],
'timestamp' => time(),
'type' => 'listcast',
'device_tokens' => trim(implode(',', $device_tokens),','),
'payload' => array(
'aps' => array(
'alert' => $text,
),
'test' => '友盟',
),
'description' => 'listcast',//描述
'production_mode' => 'false',//测试,上线为true
);
return json_encode($temp_arr);
}
/*
拼接curl请求
*/
private function _curl($url,$post) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErrNo = curl_errno($ch);
$curlErr = curl_error($ch);
curl_close($ch);
if ($httpCode == "0")
die("Curl error number:" . $curlErrNo . " , Curl error details:" . $curlErr . "\r\n");
else if ($httpCode != "200")
die("Http code:" . $httpCode . " details:" . $result . "\r\n");
else
return (array)json_decode($result);
}
//require_once __DIR__ . '/vendor/autoload.php';
//$conf = array(
//'appkey' => '534ce13b56240b219b00106d', //按照友盟后台填写
//'app_master_secret' => 'j96jq7s8vfdt9ldhsrivjxkzbwezi4uv', //按照友盟后台填写
//);
//$o = new \Umeng\Message\Client($conf);
广播
//$data = array(
//'title' => '广播标题',
//'text' => '友盟测试:broadcastNotification 这是一条广播',
//);
//$r = $o->broadcastNotification($data);
//var_dump($r);
按省发通知
//$data = array(
//'title' => '河南欢迎你',
//'text' => '燕郊的别野、驴肉的火烧,这是另一种生活方式',
//'province' => '河北',
//);
//$r = $o->sendLbsNotification($data);
//var_dump($r);
//其他功能的使用方法:请参考 tests/
}