laravel框架使用极光推送消息操作示例
本文实例讲述了laravel框架使用极光推送消息。分享给大家供大家参考,具体如下:
最近需要使用极光推送往客户端推消息,所以这里记录下使用过程。
极光推送的服务端文档:
https://docs.jiguang.cn/jpush/server/push/server_overview/
极光推送服务端PHP代码:
https://github.com/jpush/jpush-api-php-client
在laravel项目下安装极光推送
composerrequirejpush/jpush
我们在config目录下创建一个jpush.php文件,用于获取key和secret
env('JPUSH_APP_KEY',''), 'master_secret'=>env('JPUSH_MASTER_SECRET',''), 'apns_production'=>env('JPUSH_APNS_PRODUCTION',true), ];
然后在.env文件中配置相应参数
JPUSH_APP_KEY= JPUSH_MASTER_SECRET= JPUSH_APNS_PRODUCTION=true
然后我们在app目录下,创建一个Services目录,并创建JPushService.php
push(); //设置平台 $push->setPlatform($platform); switch($type){ caseself::PUSH_TYPE_ALL: $push->addAllAudience(); break; caseself::PUSH_TYPE_TAG: $push->addTag($tag); break; caseself::PUSH_TYPE_ALIAS: $push->addAlias($alias); break; caseself::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)); } } /** *获取指定设备的别名和标签 */ publicstaticfunctiongetDevices($reg_id) { $response=self::getInstance()->device()->getDevices($reg_id); if($response['http_code']==200){ return$response['body']; } return[]; } /** *给指定设备添加标签 */ publicstaticfunctionaddTags($reg_id,$tags=[]) { $response=self::getInstance()->device()->addTags($reg_id,$tags); if($response['http_code']==200){ returntrue; } returnfalse; } /** *清空指定设备的标签 */ publicstaticfunctionclearTags($reg_id) { $response=self::getInstance()->device()->clearTags($reg_id); if($response['http_code']==200){ returntrue; } returnfalse; } /** *清空指定设备的标签 */ publicstaticfunctionremoveTags($reg_id,$tags=[]) { $response=self::getInstance()->device()->removeTags($reg_id,$tags); if($response['http_code']==200){ returntrue; } returnfalse; } /** *更新指定设备的别名 */ publicstaticfunctionupdateAlias($reg_id,$alias) { $response=self::getInstance()->device()->updateAlias($reg_id,$alias); if($response['http_code']==200){ returntrue; } returnfalse; } }
创建完后,我们就可以在项目中调用JPushService::pushNotify()来推消息了。
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来区分不同的别名):
JPushService::updateAlias($user->jpush_reg_id,'user_id_'.$user->id);
置空别名:
JPushService::updateAlias($user->jpush_reg_id,'');
通过别名发送:
JPushService::pushNotify([ 'title'=>'测试', 'content'=>'测试', 'alias'=>'user_id_'.$message->receive_id, 'extras'=>$extras, 'type'=>JPushService::PUSH_TYPE_ALIAS, ]);
更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。