Laravel 极光推送
- composer引用极光的文件
composer require jpush/jpush
这个操作可能出现引入不进包,查看原因,是因为php.ini的配置问题,还是因为composer的问题,我引用的时候把php.ini的limit的限制冲256MB改成了-1,然后 composer require jpush/jpush之后又继续composer update一次。
引用好了jpush包之后,在vendor文件夹可以找到,直接把jpush文件夹丢进去并没有用,没有经过composer require jpush/jpush设置laravel很多地方没有建立好和极光的联系。
主要代码可以都写在一个文件里,然后其他文件可以引用这个文件调用方法即可。
代码如下:
<?php
namespace App\Http\Controllers\Api;
use JPush\Client as JPushClient;
use Illuminate\Http\Request;
class JpushController extends BaseController
{
public function __construct(){
$this->request = request();
$this->app_key="****";
$this->master_secret="*****";
$this->client=new JPushClient($this->app_key, $this->master_secret);
}
/** ===================================================调用方法=================================================== */
//获取alias和tags
public function getDevices($registrationID){
$result = $this->client->device()->getDevices($registrationID);
return $result;
}
//添加tags
public function addTags($registrationID, $tags){
$result = $this->client->device()->addTags($registrationID,$tags);
return $result;
}
//移除tags
public function removeTags($registrationID, $tags){
$result = $this->client->device()->removeTags($registrationID,$tags);
return $result;
}
//标签推送
public function push($tag, $alert){
$tags = explode(",", $tag);
$response = $this->client->push()
->setPlatform(array('ios', 'android'))
->addTag($tags) //标签
->setNotificationAlert($alert) //内容
->send();
// 写记录
\DB::table('jpush_msg')->insert([
'sendno' =>$response['body']['sendno'],
'msg_id' => $response['body']['msg_id'],
'http_code' => $response['http_code'],
'date' => $response['headers']['date'],
'x-jpush-timestamp'=>$response['headers']['x-jpush-timestamp'],
'server'=>$response['headers']['server'],
'type'=>'标签'
]);
return $response;
}
//更新指定设备的别名
public function updateAlias($reg_id, $alias) {
$response = $this->client->device()->updateAlias($reg_id, $alias);
if ($response['http_code'] == 200) {
return $response;
}
return false;
}
//别名推送
// public function aliasPush($alias, $alert){
// $alias = explode(",",$alias);
// $response = $this->client->push()
// ->setPlatform(array('ios', 'android'))
// ->addAlias($alias) //别名
// ->setNotificationAlert($alert) //内容
// ->send();
// return $response;
// }
public function aliasPush($alias, $alert,$content,$extras){
$alias = explode(",",$alias);
$response = $this->client->push()
->setPlatform(array('ios', 'android'))
->addAlias($alias) //别名
// ->setNotificationAlert($alert) //内容
->iosNotification(['title' => $alert, 'body' => $content], [
'sound' => 'sound',
'badge' => '+1',
'extras' => $extras,
'content-available'=>true,
'mutable-content'=>true,
])
//Android
->androidNotification($alert, [
'title' => $content,
'extras' => $extras
])
//是否生产环境
->options(array(
'apns_production' => false, // 开发环境 仅对IOS的推送有效
'time_to_live'=>10000
))
->send();
// 写记录
\DB::table('jpush_msg')->insert([
'sendno' =>$response['body']['sendno'],
'msg_id' => $response['body']['msg_id'],
'http_code' => $response['http_code'],
'date' => $response['headers']['date'],
'x-jpush-timestamp'=>$response['headers']['x-jpush-timestamp'],
'server'=>$response['headers']['server'],
'type'=>'别名'
]);
return $response;
}
public function regidPush($regid, $alert,$content,$extras){
// $alias = explode(",",$alias);
$response = $this->client->push()
->setPlatform(array('ios', 'android'))
->addRegistrationId($regid) //别名
// ->setNotificationAlert($alert) //内容
->iosNotification(['title' => $alert, 'body' => $content], [
'sound' => 'sound',
'badge' => '+1',
'extras' => $extras,
'content-available'=>true,
'mutable-content'=>true,
])
//Android
->androidNotification($alert, [
'title' => $content,
'extras' => $extras
])
//是否生产环境
->options(array(
'apns_production' => false, // 开发环境 仅对IOS的推送有效
'time_to_live'=>10000
))
->send();
// 写记录
\DB::table('jpush_msg')->insert([
'sendno' =>$response['body']['sendno'],
'msg_id' => $response['body']['msg_id'],
'http_code' => $response['http_code'],
'date' => $response['headers']['date'],
'x-jpush-timestamp'=>$response['headers']['x-jpush-timestamp'],
'server'=>$response['headers']['server'],
'type'=>'设备ID'
]);
return $response;
}
//通知
public function pushNotice($message){
$result = $client->push()
->setPlatform('all')
->addAllAudience()
->setNotificationAlert($message) //你要推送的信息
->send();
return $result;
}
/** ===================================================调用方法=================================================== */
}
我在做这个极光推送的时候主要查看的文章是php极光推送详解过程
大家可以多参考多实验,一定会搞定的。