整合极光推送 定时推送

网友投稿 1283 2022-11-27

整合极光推送 定时推送

整合极光推送 定时推送

前端,后端,极光推送之间的逻辑关系

(误区:php与前端app要对接,其实不要直接对接,php直接调用极光服务器就可以了)

一、安装jpush 极光推送  在composer的配置文件中加入

"require": { "jpush/jpush": "^3.5"}

使用composer 安装极光推送  cmd 命令

composer install  或者 composer update   1 安装依赖以后 进入步骤二。

/** * @param $content 推送的内容。 * @param $receiver 接受者信息 * @param string $m_type 推送附加字段的类型(可不填) * @param string $m_txt 推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。 * @return bool|mixed|string */ public static function send_msg($title= "",$content='',$receiver='all',$m_type='',$m_txt='',$m_time){ require '../vendor/jpush/jpush/autoload.php';//引入极光推送扩展 这里的jpush.jpush是你的vendor下极光推送的安装目录。 $config = '';//配置你的相关配置包括key secret 离线时间 发送环境等;注意是个数组。 $jpush = \Yii::$app->params['jpush']; $app_key = $jpush['jpush_key']; $m_time = $jpush['jpush_time']; $master_secret = $jpush['jpush_secret']; $base64=base64_encode("$app_key:$master_secret"); $header=array("Authorization:Basic $base64","Content-Type:application/json"); $data = array(); $data['platform'] = 'all'; //目标用户终端手机的平台类型android,ios,winphone $data['audience'] = $receiver; //目标用户 $data['notification'] = array( //统一的模式--标准模式 "alert"=>$content, //安卓自定义 "android"=>array( "alert"=>$content, "title"=>$title, "builder_id"=>1, "extras"=>array("type"=>$m_type, "txt"=>$m_txt) ), //ios的自定义 "ios"=>array( // "alert"=>$content, "badge"=>"1", "sound"=>"default", // "extras"=>array("type"=>$m_type, "txt"=>$m_txt) ), ); //苹果自定义---为了弹出值方便调测 $data['message'] = array( "msg_content"=>$content, "extras"=>array("type"=>$m_type, "txt"=>$m_txt) ); //附加选项 $data['options'] = array( "sendno"=>time(),//推送序号 "time_to_live"=>$m_time, //保存离线时间的秒数默认为一天 "apns_production"=>$jpush['jpush_ambient'], //指定 APNS 通知发送环境:0开发环境,1生产环境。 ); $param = json_encode($data); $res = self::push_curl($param,$header,$jpush['jpush_url']); if($res){ //得到返回值--成功已否后面判断 return $res; }else{ //未得到返回值--返回失败 return false; } } //推送的Curl方法 public static function push_curl($param="",$header="",$postUrl) { if (empty($param)) { return false; } $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); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); // 增加 HTTP Header(头)里的字段 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); //运行curl curl_close($ch); return $data; }

配置

//极光配置 'jpush' => [ 'jpush_key' => '', 'jpush_secret' => '', 'jpush_ambient' => '0', 'jpush_url' => ' ],

控制器

public function actionTest(){ $request = \Yii::$app->request; if ($request->isPost) { $attributes = $request->post(); $contnet = isset($attributes['contnet']) ? $attributes['contnet'] : ''; $times = isset($attributes['time']) ? $attributes['time'] : ''; $times = (int)$times; $list =JpushServer::send_msg($contnet,"all","","",$times); return $list; } else { return $this->returnData('', -1, '请求方式错误'); } }

前端准备的工作:

在用户登录时做以下处理 1.获取应用设备id,给应用添加别名或标签(记得一定要有要不然提示1011) 2.将设置的别名传到极光服务器 3.将用户信息与对应的设备号,别名,登录状态等信息存到公司的服务器中,以便他用

最后,我用的是定时器任务去触发任务。不知道你们用的是什么方法来触发消息通知任务。

下面是定时推送接口,按照官方文档走就可以了

调用

//定时任务推送 $send_time =date('Y-m-d H:i:s',$times); $res = JpushServer::send_msg_sc($bulletin->title,$bulletin->desc,"all","","",$times,$send_time);

封装方法

/** * @param string $title * @param string $content * @param string $receiver * @param string $m_type * @param string $m_txt * @param $m_time * @param $send_time 推送时间 * @return bool|mixed * 极光定时推送 */ public static function send_msg_sc($title= "",$content='',$receiver='all',$m_type='',$m_txt='',$m_time,$send_time){ $config = '';//配置你的相关配置包括key secret 离线时间 发送环境等;注意是个数组。 $jpush = \Yii::$app->params['jpush']; $app_key = $jpush['jpush_key']; $m_time = $jpush['jpush_time']; $jpush_url_schedules = $jpush['jpush_url_schedules']; $master_secret = $jpush['jpush_secret']; $base64=base64_encode("$app_key:$master_secret"); $header=array("Authorization:Basic $base64","Content-Type:application/json"); $data = array(); $data["name"] = $title; $data["enabled"] = true; $data["trigger"] = array( "single"=>array( "time"=>$send_time ), ); $data["push"] = array( "platform" => "all", "audience" => "all", "notification"=>array( "alert"=> $content ), "message"=>array( "msg_content"=> $content ), "options"=>array( "time_to_live"=> $m_time ), ); $param = json_encode($data); $res = self::push_curl($param,$header,$jpush_url_schedules); if($res){ //得到返回值--成功已否后面判断 return $res; }else{ //未得到返回值--返回失败 return false; } } //推送的Curl方法 public static function push_curl($param="",$header="",$postUrl) { if (empty($param)) { return false; } $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); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); // 增加 HTTP Header(头)里的字段 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $data = curl_exec($ch); //运行curl curl_close($ch); return $data; }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PHP从数组中删除元素的四种方法实例
下一篇:python 安装httplib2 0.9.2 或使用pip安装
相关文章

 发表评论

暂时没有评论,来抢沙发吧~