<?phpnamespace app\service;use think\facade\Cache;/*** Class SinglePushService* 个推文档 https://docs.getui.com/getui/server/rest_v2/push/* @package app\service* 个推* thinkphp 6*/
class SinglePushService
{protected $appId = 'xxx';   // 个推 appidprotected $appKey = 'xxx';    // 个推 appKeyprotected $appSecret = 'xxx';    // 个推 appSecretprotected $masterSecret = 'xxx';  // 个推 masterSecretprotected $baseUrl = 'https://restapi.getui.com/v2/';  // 个推 base urlprotected $pushUrl = '/push/single/cid';   // 个推 urlprotected $tokenUrl = '/auth';  // 个推 token url/*** singlePush* 个推 post请求* @param string $title    // 标题* @param string $body // 内容* @param string $payload  // 参数 如跳转的url* @param string $cid 用户cid,使用plus.push.getClientInfoAsync函数获取,可在uni官网找到* @param int $scheduleTime 毫秒* ps 这些方法是从个推sdk demo剥离出来的,非个推sdk方法*/public function singlePush($title, $body, $payload, $cid, $scheduleTime){$token = $this->token();  // token 存redis$url = $this->baseUrl . $this->appId . $this->pushUrl;$header = ['token: ' . $token];$intent = "intent:#Intent;scheme=unipush;launchFlags=0x4000000;component={你的包名}/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={$title};S.content={$body};S.payload={$payload};end";$params = ['request_id' => uniqid(),  // 随便一个唯一数'audience' => ['cid' => [$cid]],'settings' => ['ttl' => 3600 * 24 * 1000,// 毫秒'schedule_time' => $scheduleTime    // 定时推送时间 这个玩意要vip ,非vip可以注释掉],// 在线消息推送'push_message' => ['notification' => ['title' => $title,'body' => $body,'logo' => 'logo.png',  // logo name'logo_url' => 'https:www.xxx.com.logo.png', // logo url'click_type' => 'intent','intent' => $intent,'channel_level' => 4]],// 离线消息'push_channel'=>['android'=>['ups'=>['notification'=>['title'=>$title,'body'=>$body,'click_type' => 'intent','intent' => $intent]]]]];$rest = self::httpRequest($url, $params, $header, 'POST');return $rest;}/*** token* 获取token*/private function token(){$key = 'single_token';$redis = Cache::store('redis');$token = $redis->get($key);if ($token) return $token; // 有就直接返回 token,没有就去请求token$url = $this->baseUrl . $this->appId . $this->tokenUrl;$params = ['sign' => $this->hash(),    // sha256'timestamp' => $this->mTime(),    // 毫秒13位时间戳'appkey' => $this->appKey,];$rest = self::httpRequest($url, $params, null, 'POST');if ($rest['msg'] == 'success') {$token = $rest['data']['token'];$expire = $rest['data']['expire_time'];   // 毫秒13位时间戳$substr = substr($expire, 0, 10);$ttl = bcsub($substr, time());    // 计算过期时间,存入redis$redis->set($key, $token, $ttl);return $token;}return false;}/*** mTime* 毫秒*/public function mTime(){list($mse, $sec) = explode(' ', microtime());$mtime = (float)sprintf('%.0f', (floatval($mse) + floatval($sec)) * 1000);return $mtime;}/*** hash*/private function hash(){$str = $this->appKey . $this->mTime() . $this->masterSecret;return hash('sha256', $str);}/*** @param $url* @param $data* @param $method* @param $headers* request* 发起请求 curl*/private static function request($url, $data, $method, $headers){$curl = curl_init($url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);curl_setopt($curl, CURLOPT_USERAGENT, 'GeTui RAS2 PHP/1.0');curl_setopt($curl, CURLOPT_FORBID_REUSE, 0);curl_setopt($curl, CURLOPT_FRESH_CONNECT, 0);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 60000);curl_setopt($curl, CURLOPT_TIMEOUT_MS, 30000);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);$header = null;if ($headers != null) {array_push($headers, "Content-Type:application/json;charset=UTF-8", "Connection: Keep-Alive");$header = $headers;} else {$header = array("Content-Type:application/json;charset=UTF-8", "Connection: Keep-Alive");}curl_setopt($curl, CURLOPT_HTTPHEADER, $header);switch ($method) {case 'GET':curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');break;case 'POST':curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);break;case 'DELETE':curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');curl_setopt($curl, CURLOPT_POSTFIELDS, $data);break;case 'PUT':curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式curl_setopt($curl, CURLOPT_POSTFIELDS, $data);break;}$curl_version = curl_version();if ($curl_version['version_number'] >= 462850) {curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 30000);curl_setopt($curl, CURLOPT_NOSIGNAL, 1);}//请求失败有3次重试机会$result = self::exeBySetTimes(3, $curl);return $result;}/*** @param $url* @param $params* @param $headers* @param $method* httpRequest* 个推*/public static function httpRequest($url, $params, $headers, $method){$data = json_encode($params);$result = null;try {$resp = self::request($url, $data, $method, $headers);$result = json_decode($resp, true);return $result;} catch (\Exception $e) {return $e->getMessage();}}/*** @param int $count 重试次数* @param $curl* exeBySetTimes* 重试*/private static function exeBySetTimes($count, $curl){$result = curl_exec($curl);$info = curl_getinfo($curl);$code = $info["http_code"];if (curl_errno($curl) != 0 && $code != 200) {$count--;if ($count > 0) {$result = self::exeBySetTimes($count, $curl);} else {if ($code == 0 || $code == 404 || $code == 504) {return false;}}}return $result;}
}

uniapp消息推送(个推-PHP服务端推送)相关推荐

  1. Java 服务端推送消息有那么难吗?

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 转自公众号:码农小胖哥 今天项目经理交给我一个开发任务.如果有人在前台下了订单就给后台仓库管 ...

  2. nett服务器接收消息的方法,C#(一沙框架) .net core3.1 SignalR 服务端推送消息至客户端的实现方法,用弹窗插件进行显示,非常美观实用...

    C#(一沙框架) .net core3.1 SignalR 服务端推送消息至客户端的实现方法,用弹窗插件进行显示,非常美观实用 运行效果: 1.安装Microsoft.AspNetCore.Signa ...

  3. springboot 实现服务端推送消息

    文章目录 前言 一.关于SSE 1. 概念介绍 2. 特点分析 3. 应用场景 二.SpringBoot实现 三.前端vue调用 四.一些问题 前言 服务端推送消息我们采用SSE方式进行推送. 一.关 ...

  4. springboot实现SSE服务端主动向客户端推送数据,java服务端向客户端推送数据,kotlin模拟客户端向服务端推送数据

    SSE服务端推送 服务器向浏览器推送信息,除了 WebSocket,还有一种方法:Server-Sent Events(以下简称 SSE).本文介绍它的用法. 在很多业务场景中,会涉及到服务端向客户端 ...

  5. springboot 之 webscoket 服务端推送

    因为最近有后端实时推送数据的需求,所以想到了websocket组件,在此写一下springboot集成使用websocket的方法,供各位童鞋参考. 注:基于test项目. 1.首先打开pom.xml ...

  6. PHP服务端推送技术Long Polling

    perfgeeks linux . bash . php . python . c PHP服务端推送技术Long Polling Long Polling与Polling概述 服务端推送技术应用越来越 ...

  7. JAVA实现QQ:实现文字聊天、QQ用户登录、拉取在线用户列表、无异常退出、私聊、发文件、下载文件、离线留言、服务端推送新闻等功能(后端无界面,Utilty源码在后面、)

    这个仿QQ项目是参考韩顺平老师的多线程课程做的,因为个人觉得非常有意义特别是让我对多线程通信又了一个新的理解因此我准备写一篇总结(如果觉得视频太长可以参考下): 具体视频地址:大家给韩老师一键三连[韩 ...

  8. appollo消息服务器,Springboot 集成 MQTT —— web 服务端实现(apollo 客户端)-Go语言中文社区...

    基于 MQTT 可以实现很多场景,例如现在使用比较多的物联网,还有消息的实时推送.联网的设备连接上 apollo 服务器以后,一直监听 apollo 推送过来的信令/消息即可. 1.web 服务端向联 ...

  9. mqtt 发送消息过多_阿里云MQTT服务端注解式消息处理分发与同步调用实践小结

    一.前言 前段时间公司预研了设备app端与服务端的交互方案,出于多方面考量最终选用了阿里云的微服务队列MQTT方案,基于此方案,本人主要实践有: 1. 封装了RocketMQ实现MQTT订阅与发布的实 ...

  10. 小程序表单提交,服务端推送模板消息通知

    1.小程序按钮表单,提交formid和openid 注:https://blog.csdn.net/qq_38191191/article/details/80982732 2.发送网络请求(小程序点 ...

最新文章

  1. libACE-6.3.3.so: cannot open shared object file: No such file or directory
  2. LFSR 和 m序列
  3. 【转】C++标准转换运算符static_cast
  4. linux运行apktool签名,解决Linux中使用ApkTool遇到问题
  5. [贪心算法] 例6.2 今年暑假不AC
  6. Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心
  7. 2、深入理解 Laravel Eloquent(二)——中间操作流(Builder)
  8. 100款机器学习数据集
  9. Unicode和UTF编码转换
  10. doc和docx、xls和xlsx、ppt和pptx有什么区别?
  11. python TCP服务器v1.4 - 客户端连接服务器异常(异常情况分类)处理
  12. 蘑菇战争2显示没有连接服务器,蘑菇战争2新手怎么玩?四步带你解决新手难关[多图]...
  13. 多测师_swipe 滑动操作
  14. python 怎么建立文件夹_如何用PYTHON新建文件夹
  15. JavaWeb(Linux)
  16. ArcGIS 切片/瓦片的发布与加载
  17. 易签到PHP源码,会议签到系统(会易签到)应用项目源码
  18. python排序输出人名,005_015 Python 人名按字母排序,首字母分组
  19. Java EE入门教程系列第一章Java EE的概述(一)——初识Java EE
  20. matlab自耦变压器,基于MATLAB的500kV自耦变压器建模及仿真.pdf

热门文章

  1. Win10恢复照片查看器
  2. 实体门店的促销活动该如何策划才能成功?
  3. jadx反编译程序未响应
  4. 大一新生必看,自学必看,里昂详解数据结构之二叉树
  5. HDU CCPC网络选拔赛 6441 Find Integer(数学)
  6. DRM驱动(五)之drm_atomic_state
  7. RN导入高德地图定位的用法实例
  8. 让AI做作业:基于PaddleNLP-Taskflow的错别字单项测试
  9. proftpd 530 Login incorrect
  10. Java 并发编程(一) 学习教程