thinkphp 写APP接口集成极光推送的例子

下面我们来看一篇关于thinkphp 写APP接口集成极光推送,对于app的数据都得通过接口来实现了,当然也有内置数据库的不过这种非常少了。
最近用Thinkphp写了个App接口用到第三方推送功能,本文用的第三方推送工具是极光推送,其他的推送不一一介绍。
第一步:下载PHPSDK 并到官网上注册应用将app_key 、master_secret 放到文件中
下载地址:https://www.jpush.cn/common/downloads/resource/1460966988359
第二步:将解压后的文件 src->JPush中的文件及文件夹复制到ORG->Push中(文件夹需要自己创建)或者放到vender中
第三步:在公共控制器CommonAction中创建push方法
private $app_key=****;
private $master_secret=****;
public function push()

import("ORG.Push.Push");
$client = new JPush($this->app_key,$this->master_secret);
$result = $client->push()
->setPlatform('all')
->addAllAudience()
->setNotificationAlert("这是测试的推送")
->send();
//echo 'Result=' . json_encode($result) . $br;

这样就可以完成基本的推送
其他功能本项目没有涉及到就没有书写了解更多:
https://github.com/jpush/jpush-api-php-client/blob/master/doc/api.md#device-api
======华丽的分格线======
另一种方式:
<?php
//jpush.php 这是推送方法 用到curl发送请求
class jpush {
private $_masterSecret = '';
private $_appkeys = '';

/**
* 构造函数
* @param string $username
* @param string $password
* @param string $appkeys
*/
function __construct($masterSecret = '',$appkeys = '') {
$this->_masterSecret = $masterSecret;
$this->_appkeys = $appkeys;
}
/**
* 模拟post进行url请求
* @param string $url
* @param string $param
*/
function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}

$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);

return $data;
}
/**
* 发送
* @param int $sendno 发送编号。由开发者自己维护,标识一次发送请求
* @param int $receiver_type 接收者类型。1、指定的 IMEI。此时必须指定 appKeys。2、指定的 tag。3、指定的 alias。4、 对指定 appkey 的所有用户推送消息。
* @param string $receiver_value 发送范围值,与 receiver_type相对应。 1、IMEI只支持一个 2、tag 支持多个,使用 "," 间隔。 3、alias 支持多个,使用 "," 间隔。 4、不需要填
* @param int $msg_type 发送消息的类型:1、通知 2、自定义消息
* @param string $msg_content 发送消息的内容。 与 msg_type 相对应的值
* @param string $platform 目标用户终端手机的平台类型,如: android, ios 多个请使用逗号分隔
*/
function send($sendno = 0,$receiver_type = 1, $receiver_value = '', $msg_type = 1, $msg_content = '', $platform = 'android,ios') {
$url = 'http://api.jpush.cn:8800/sendmsg/v2/sendmsg';
$param = '';
$param .= '&sendno='.$sendno;
$appkeys = $this->_appkeys;
$param .= '&app_key='.$appkeys;
$param .= '&receiver_type='.$receiver_type;
$param .= '&receiver_value='.$receiver_value;
$masterSecret = $this->_masterSecret;
$verification_code = md5($sendno.$receiver_type.$receiver_value.$masterSecret);
$param .= '&verification_code='.$verification_code;
$param .= '&msg_type='.$msg_type;
$param .= '&msg_content='.$msg_content;
$param .= '&platform='.$platform;
$res = $this->request_post($url, $param);
if ($res === false) {
return false;
}
$res_arr = json_decode($res, true);
return $res_arr;
}

}
?>
调用方式:
<?php
include('jpush.php');
$n_title = '驿泊';
$n_content = '驿泊人生';
$arr=array('fromer'=>'发送者','fromer_name'=>'发送者名字','fromer_icon'=>'发送者头像','image'=>'发送图片链接','sound'=>'发送音乐链接');//自定义参数
$appkeys='先上传app应用项目,自动生成的key';
$masterSecret='appkey下边就同样生成mastersecret的秘钥';
$sendno = 4;
$receiver_value = '';
$platform = 'Android,iOS' ;
$msg_content = json_encode(array('n_builder_id'=>0, 'n_title'=>$n_title, 'n_content'=>$n_content,'n_extras'=>$arr));
$obj = new jpush($masterSecret,$appkeys);
$res = $obj->send($sendno, 4, $receiver_value, 1, $msg_content, $platform);
print_r($res);
exit();
?>

猜你喜欢

转载自www.cnblogs.com/liushuang1/p/11204916.html
今日推荐