当前位置:首页 > PHP > 正文内容

lavel对接个推别名推送

陈杰2年前 (2021-09-24)PHP3110

新项目采用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();

       //创建APIAPPID等配置参考 环境要求 进行获取
       $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);
       //点击通知后续动作,目前支持以下后续动作:
       //1intent:打开应用内特定页面url:打开网页地址。2payload:自定义消息内容启动应用。3payload_custom:自定义消息内容不启动应用。4startapp:打开应用首页。5none:纯通知,无后续动作
       $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()中的方法

image.png

看一下个推的官方文档,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的通道

image.png


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的参数吧

image.png

    //安卓
        $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

运行队列就行了。

扫描二维码至手机访问

扫描二维码推送至手机访问。

版权声明:本文由何烦过虎溪发布,如需转载请注明出处。

转载请注明出处:http://95shouyou.com/?id=42

分享给朋友:

相关文章

laravel通过模板html生成word文档

laravel通过模板html生成word文档

因为公司项目需要,内部的管理系统有一个php实时查询数据库,根据内容来生成word文档排版没有什么需求,实现原理是查询数据库,然后用laravel自带的模板引擎,生成一个html网页,然后修改网页的r...

通过supervisor管理laravel的queue队列

通过supervisor管理laravel的queue队列

配置文件[program:queue]command=php artisan queue:work redis --tries=3 --delay=3directory=/www/wwwroot/sh...

php接拼多多的多多进宝,实现淘客功能

php接拼多多的多多进宝,实现淘客功能

在项目中有一个板块是接拼多多的多多进宝,实现多多客的功能,主要就是拉取商品列表,拉取商品详情,生成推广链接。composer require justmd5/duoduoke-sd...

laravel分表model映射的思路

预先估计会出现大数据量并且访问频繁的表,将其分为若干个表这种预估大差不差的,论坛里面发表帖子的表,时间长了这张表肯定很大,几十万,几百万都有可能。 聊天室里面信息表,几十个人在一起一聊一个晚上,时间长...

laravel实现微信公众号授权登录实战

微信公众号授权登录实战框架:laravel依赖:overtrue/wechat首先安装一下easywechat依赖composer require overtrue/wechat:...

Laravel记录SQL操作日志的方法

Laravel记录SQL操作日志的方法

在laravel中我们有一个需求就是,涉及到sql操作的update,insert,delete操作的语句,我们都要写一个日志来记录一下说说方法吧。在项目目录app/Providers/AppServ...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。