最近需要使用极光推送往客户端推消息,所以这里记录下使用过程。
极光推送的服务端文档:
1 | https: //docs.jiguang.cn/jpush/server/push/server_overview/ |
极光推送服务端PHP代码:
1 | https: //github.com/jpush/jpush-api-php-client |
在laravel项目下安装极光推送
1 | composer require jpush/jpush |
我们在config目录下创建一个jpush.php文件,用于获取key和secret
1 2 3 4 5 6 7 | <?php return [ 'app_key' => env( 'JPUSH_APP_KEY' , '' ), 'master_secret' => env( 'JPUSH_MASTER_SECRET' , '' ), 'apns_production' => env( 'JPUSH_APNS_PRODUCTION' , true), ]; |
然后在 .env 文件中配置相应参数
1 2 3 | JPUSH_APP_KEY= JPUSH_MASTER_SECRET= JPUSH_APNS_PRODUCTION=true |
然后我们在app目录下,创建一个 Services目录,并创建JPushService.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | <?php namespace App\Services; use JPush\Client as JPush; use Log; class JPushService { 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 (!self:: $client ) { self:: $client = new JPush(config( 'jpush.app_key' ), config( 'jpush.master_secret' ), null); } return self:: $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 = self::getInstance()->push(); //设置平台 $push ->setPlatform( $platform ); switch ( $type ) { case self::PUSH_TYPE_ALL: $push ->addAllAudience(); break ; case self::PUSH_TYPE_TAG: $push ->addTag( $tag ); break ; case self::PUSH_TYPE_ALIAS: $push ->addAlias( $alias ); break ; case self::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, ]); $response = $push ->send(); if ( $response [ 'http_code' ] != 200) { Log::channel( 'jpush' )->error(json_encode( $response , JSON_UNESCAPED_UNICODE)); } return $response ; } catch (\Throwable $e ) { Log::channel( 'jpush' )->error(json_encode([ 'file' => $e ->getFile(), 'line' => $e ->getLine(), 'message' => $e ->getMessage(), 'params' => $params , ], JSON_UNESCAPED_UNICODE)); } } /** * 获取指定设备的别名和标签 */ public static function getDevices( $reg_id ) { $response = self::getInstance()->device()->getDevices( $reg_id ); if ( $response [ 'http_code' ] == 200) { return $response [ 'body' ]; } return []; } /** * 给指定设备添加标签 */ public static function addTags( $reg_id , $tags = []) { $response = self::getInstance()->device()->addTags( $reg_id , $tags ); if ( $response [ 'http_code' ] == 200) { return true; } return false; } /** * 清空指定设备的标签 */ public static function clearTags( $reg_id ) { $response = self::getInstance()->device()->clearTags( $reg_id ); if ( $response [ 'http_code' ] == 200) { return true; } return false; } /** * 清空指定设备的标签 */ public static function removeTags( $reg_id , $tags = []) { $response = self::getInstance()->device()->removeTags( $reg_id , $tags ); if ( $response [ 'http_code' ] == 200) { return true; } return false; } /** * 更新指定设备的别名 */ public static function updateAlias( $reg_id , $alias ) { $response = self::getInstance()->device()->updateAlias( $reg_id , $alias ); if ( $response [ 'http_code' ] == 200) { return true; } return false; } } |
创建完后,我们就可以在项目中调用 JPushService::pushNotify() 来推消息了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | JPushService::pushNotify([ //标题 'title' => '测试' , //内容 'content' => '测试' , //设备标识,跟设备相关 'reg_id' => 'xxxxxxxxxxx' , //扩展字段 'extras' => [ 'key' => 'value' , ], //推送类型 'type' => JPushService::PUSH_TYPE_REG_ID, ]); |
reg_id是前端安卓或IOS获取到后,传给PHP后端,然后跟用户关联,存起来。
注意,reg_id是跟设备相关的,同一个设备上的APP,当不同用户登陆时,reg_id是一样的,这样会导致一个问题。
A用户登APP后,又切换到B用户,那B用户会收到发送给A用户的消息,这会造成消息错乱。
解决方法:
通过别名来发送消息,因为一个设备只能绑定一个别名,当A用户登陆时,把 reg_id 绑定到别名 user_a,切换用户或退出时,就把别名置空。
然后B用户登陆,就把 reg_id 绑定到 user_b 上。推消息时,就通过别名来推送消息。
绑定别名(推荐使用用户ID来区分不同的别名):
1 | JPushService::updateAlias( $user ->jpush_reg_id, 'user_id_' . $user ->id); |
置空别名:
1 | JPushService::updateAlias( $user ->jpush_reg_id, '' ); |
通过别名发送:
1 2 3 4 5 6 7 | JPushService::pushNotify([ 'title' => '测试' , 'content' => '测试' , 'alias' => 'user_id_' . $message ->receive_id, 'extras' => $extras , 'type' => JPushService::PUSH_TYPE_ALIAS, ]); |
版权声明:博主文章,可以不经博主允许随意转载,随意修改,知识是用来传播的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决