lavel对接个推别名推送
新项目采用uniapp开发,需要用到推送功能,之前接过极光,其中的一些坑都踩过了,但是unipush用的是个推的推送,那就只有重写踩坑了。
composer require getuilaboratory/getui-pushapi-php-client-v2
老规矩,composer加载一下个推的官方sdk吧,社区里面有很多个人封装的sdk,想了一下还是用官方的吧,虽然麻烦一些。
还是用到laravel自带的异步队列。
继续码代码,新建一个PushJob.php
代码全部内容为,下面我们在具体分析
<?php
/**
* Author:陈杰
* Blog:http://blog.95shouyou.com
* Email:823380606@qq.com
* Git:https://gitee.com/chen95
* Date:2021/9/22 0022
* Time:11:57
*/
namespace App\Jobs;
use App\Model\Admin\ConfigModel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class PushJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $chat = [];
public $user = [];
public function __construct($chat, $user)
{
$this->chat = $chat;
$this->user = $user;
//
}
public function handle()
{
$config = ConfigModel::get_config();
//创建API,APPID等配置参考 环境要求 进行获取
$api = new \GTClient("https://restapi.getui.com", $config["push_appkey"], $config["push_appid"], $config["push_mastersecret"]);
//设置推送参数
$push = new \GTPushRequest();
$push->setRequestId(Str::random());
$title = Str::limit($this->user["name"], 30);
switch ($this->chat["type"]) {
case 0:
$body = Str::limit($this->chat["content"]["c"], 40);
break;
case 1:
$body = "[发送了一张图片]";
break;
case 2:
$body = "[送给您礼物]";
break;
case 3:
$body = "[语音聊天申请]";
break;
case 4:
$body = "[视频聊天申请]";
break;
case 5:
$body = "[发送了一段语音]";
break;
default:
$body = "Hi~";
break;
}
$message = new \GTPushMessage();
$notify = new \GTNotification();
$notify->setTitle($title);
$notify->setBody($body);
//点击通知后续动作,目前支持以下后续动作:
//1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
$notify->setClickType("payload");
$payload = json_encode(["title" => $title, "body" => $body, "type" => "chat", "id" => $this->chat["firend_id"]]);
$notify->setPayload($payload);
$intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.lixiang.youyuanren/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={$this->user["name"]};S.content={$body};S.payload={$payload};end";
$notify->setIntent($intent);
$notify->setBadgeAddNum(1);
$message->setNotification($notify);
$pushChannel = new \GTPushChannel();
//ios
$ios = new \GTIos();
$ios->setType("notify");
$ios->setAutoBadge("1");
$ios->setPayload($payload);
//aps设置
$aps = new \GTAps();
$aps->setContentAvailable(0);
$alert = new \GTAlert();
$alert->setTitle($title);
$alert->setBody($body);
$aps->setAlert($alert);
$ios->setAps($aps);
$pushChannel->setIos($ios);
//安卓
$android = new \GTAndroid();
$ups = new \GTUps();
$thirdNotification = new \GTThirdNotification();
$thirdNotification->setTitle($title);
$thirdNotification->setBody($body);
$thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_INTENT);
$thirdNotification->setIntent($intent);
$thirdNotification->setPayload($payload);
$ups->setNotification($thirdNotification);
$android->setUps($ups);
$pushChannel->setAndroid($android);
$push->setPushMessage($message);
$push->setPushChannel($pushChannel);
// $push->set
$push->setAlias($this->chat["nid"]);
//处理返回结果
$result = $api->pushApi()->pushToSingleByAlias($push);
var_dump($result);
echo date("Y-m-d H:i:s", time()) . "\n\n";
return true;
}
}
分析一下源码,
//创建API,APPID等配置参考 环境要求 进行获取 $api = new \GTClient("https://restapi.getui.com", $config["push_appkey"], $config["push_appid"], $config["push_mastersecret"]);
这个api是最终的请求基类,肯定要首先new一下的。
然后用到pushApi()中的方法
看一下个推的官方文档,audience里面设置别名相关的,
push_message中走个推通道的相关标题和body
push_channel是走厂商通道的相关标题和body
那么一个一个的来设置吧。
首先设置push_meesage
$message = new \GTPushMessage(); $notify = new \GTNotification(); $notify->setTitle($title); $notify->setBody($body); //点击通知后续动作,目前支持以下后续动作: //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作 $notify->setClickType("payload"); $payload = json_encode(["title" => $title, "body" => $body, "type" => "chat", "id" => $this->chat["firend_id"]]); $notify->setPayload($payload); $intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.lixiang.youyuanren/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={$this->user["name"]};S.content={$body};S.payload={$payload};end"; $notify->setIntent($intent); $notify->setBadgeAddNum(1); $message->setNotification($notify);
这样就把push_message设置好了,但是这个也是只能走个推通道,在项目中实际上app在线我们已经用websocket通知了,所以肯定还得用到厂商离线通道。。注意一个坑,个推通道ios虽然有通知,但是因为没有设置aps中的alert所以虽然前端能receive到,但是不创建通知栏通知,得前端自己创建,这个下一篇文章我们再来分析。
现在来分析一下IOS的通道
push_channel中有两个参数 ios和android
那么先设置ios的
$ios = new \GTIos(); $ios->setType("notify"); $ios->setAutoBadge("1"); $ios->setPayload($payload);
ios中的参数参考个推就行了,我们重点研究aps和alert,因为只有设置了这两个东西才能正常在通知栏创建通知
//aps设置 $aps = new \GTAps(); $aps->setContentAvailable(0); // $aps->setSound("com.gexin.ios.silenc"); // $aps->setCategory("category"); // $aps->setThreadId("threadId"); $alert = new \GTAlert(); $alert->setTitle($title); $alert->setBody($body); $aps->setAlert($alert); $ios->setAps($aps); $pushChannel->setIos($ios);
这里就设置上了aps和alert,然后把ios通道加到push_channel里面去。ios这样就设置好了,再来研究android
看看android的参数吧
//安卓 $android = new \GTAndroid(); $ups = new \GTUps(); // $ups->setTransmission("ups Transmission"); $thirdNotification = new \GTThirdNotification(); $thirdNotification->setTitle($title); $thirdNotification->setBody($body); $thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_INTENT); $thirdNotification->setIntent($intent); $thirdNotification->setPayload($payload); $ups->setNotification($thirdNotification); $android->setUps($ups); $pushChannel->setAndroid($android);
安卓就简单了,只需要设置notification就可以了,透传的那个我们暂时不用。以后再研究吧。然后再把android设置到push_channel里面
现在我们的厂商通道和个推通道都设置好了,那就执行吧
$push->setPushMessage($message); $push->setPushChannel($pushChannel); $push->setAlias($this->chat["nid"]); //处理返回结果 $result = $api->pushApi()->pushToSingleByAlias($push);
因为是别名推送,所以有一个setAlias
最后我们
php artisan queue:work
运行队列就行了。
