本文实例讲述了laravel框架使用极光推送消息。分享给大家供大家参考,具体如下:
最近需要使用极光推送往客户端推消息,所以这里记录下使用过程。
极光推送的服务端文档:
https://docs.jiguang.cn/jpush/rver/push/rver_overview/
极光推送服务端php代码:
https://github.com/jpush/jpush-api-php-client
在laravel项目下安装极光推送
compor require jpush/jpush
我们在config目录下创建一个jpush.php文件,用于获取key和cret
<?phpreturn [ 'app_key' => env('jpush_app_key', ''), 'master_cret' => env('jpush_master_cret', ''), 'apns_production'穿越火线名字大全 => env('jpush_apns_production', true),];
然后在 .env 文件中配置相应参数
jpush_app_key=jpush_master_cret=jpush_apns_production=true
然后我们在app目录下,创建一个 rvices目录,并创建jpushrvice.php
<?phpnamespace a欧冠赔率pp\rvices;u jpush\client as jpush;u log;class jpushrvice{ protected static $client = null; //推送类型 const push_type_all = 1; const push_type_tag = 2; const push_type_alias = 3; const push_type_reg_id = 4; private function __construct() { } private function __clone() { } /** * 获取实例 */ public static function getinstance() { if (!lf::$client) { lf::$client = new jpush(config('jpush.app_key'), config('jpush.master_cret'), null); } return lf::$client; } /** * 给android或ios推送消息 */ public static function pushnotify($params) { //推送平台 $platform = $params['platform'] ?? 'all'; //推送标题 $title = $params['title'] ?? ''; //推送内容 $content = $params['content'] ?? ''; //通知栏样式id $builder_id = $params['builder_id'] ?? 0; //附加字段 $extras = $params['extras'] ?? ''; //推送类型 $type = $params['type'] ?? ''; //推送目标(注册id) $reg_id = $params['reg_id'] ?? ''; //推送目标(标签) $tag = $params['tag'] ?? ''; //推送目标(别名) $alias = $params['alias'] ?? ''; try { $push = lf::getinstance()->push(); //设置平台 $push->tplatform($platform); switch ($type) { ca lf::push_type_all: $push->addallaudience(); break; ca lf::push_type_今日新闻摘抄十条tag: $push->addtag($tag); break; ca lf::push_type_alias: $push->addalias($alias); break; ca lf::push_type_reg_id: $push->addregistrationid($reg_id); break; } $push->androidnotification($content, [ 'title' => $title, 'builder_id' => $builder_id, 'extras' => $extras, ])->iosnotification($content, [ 'sound' => 'sound', 'badge' => '+1', 'extras' => $extras ])->options([ 'apns_production' => config('jpush.apns_production', true), //表示离线消息保留时长(秒) 'time_to_live' => 86400, ]); $respon = $push->nd(); if ($respon['http_code'] != 200) { log::channel('jpush')->error(json_encode($respon, json_unescaped_unicode)); } return $respon; } catch (\throwable $e) { log::channel('jpush')->error(json_enc购销合同范本ode([ 'file' => $e->getfile(), 'line' => $e->getline(), 'message' => $e->getmessage(), 'params' => $params, ], json_unescaped_unicode)); } } /** * 获取指定设备的别名和标签 */ public static function getdevices($reg_id) { $respon = lf::getinstance()->device()->getdevices($reg_id); if ($respon['http_code'] == 200) { return $respon['body']; } return []; } /** * 给指定设备添加标签 */ public static function addtags($reg_id, $tags = []) { $respon = lf::getinstance()->device()->addtags($reg_id, $tags); if ($respon['http_code'] == 200) { return true; } return fal; } /** * 清空指定设备的标签 */ public static function cleartags($reg_id) { $respon = lf::getinstance()->device()->cleartags($reg_id); if ($respon['http_code'] == 200) { return true; } return fal; } /** * 清空指定设备的标签 */ public static function removetags($reg_id, $tags = []) { $respon = lf::getinstance()->device()->removetags($reg_id, $tags); if ($respon['http_code'] == 200) { return true; } return fal; } /** * 更新指定设备的别名 */ public static function updatealias($reg_id, $alias) { $respon = lf::getinstance()->device()->updatealias($reg_id, $alias); if ($respon['http_code'] == 200) { return true; } return fal; }}
创建完后,我们就可以在项目中调用 jpushrvice::pushnotify() 来推消息了。
jpushrvice::pushnotify([ //标题 'title' => '测试', //内容 'content' => '测试', //设备标识,跟设备相关 'reg_id' => 'xxxxxxxxxxx', //扩展字段 'extras' => [ 'key' => 'value', ], //推送类型 'type' => jpushrvice::push_type_reg_id,]);
reg_id是前端安卓或ios获取到后,传给php后端,然后跟用户关联,存起来。
注意,reg_id是跟设备相关的,同一个设备上的app,当不同用户登陆时,reg_id是一样的,这样会导致一个问题。
a用户登app后,又切换到b用户,那b用户会收到发送给a用户的消息,这会造成消息错乱。
解决方法:
通过别名来发送消息,因为一个设备只能绑定一个别名,当a用户登陆时,把 reg_id 绑定到别名 ur_a,切换用户或退出时,就把别名置空。
然后b用户登陆,就把 reg_id 绑定到 ur_b 上。推消息时,就通过别名来推送消息。
绑定别名(推荐使用用户id来区分不同的别名):
jpushrvice::updatealias($ur->jpush_reg_id, 'ur_id_' . $ur->id);
置空别名:
jpushrvice::updatealias($ur->jpush_reg_id, '');
通过别名发送:
jpushrvice::pus有关黄河的神话传说hnotify([ 'title' => '测试', 'content' => '测试', 'alias' => 'ur_id_' . $message->receive_id, 'extras' => $extras, 'type' => jpushrvice::push_type_alias,]);
本文发布于:2023-04-08 17:05:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/5926ab58b7e2a3a2e642e47afc16686b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:laravel框架使用极光推送消息操作示例.doc
本文 PDF 下载地址:laravel框架使用极光推送消息操作示例.pdf
留言与评论(共有 0 条评论) |