首页 >> 大全

Thinkphp5.1使用极光推送

2023-12-16 大全 28 作者:考证青年

.1使用极光推送

配置过程 开发过程

_极光推送实现原理_极光推送原理

namespace app\api\controller;use JPush\Client as JPush;
use JPush\Exceptions\APIConnectionException;
use JPush\Exceptions\APIRequestException;require_once './vendor/jpush/jpush/autoload.php';class JPushTest extends Controller
{public function test(){$app_key = "xxx";$master_secret = "xxx";//进行极光推送$client = new JPush($app_key, $master_secret);try {$response = $client->push()->setPlatform(array('ios', 'android'))// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求->addAlias('xxxx')->addTag(array('xxx'))//->addRegistrationId($jpush_key)//$jpush_key此处jpush_key是手机端的//->setNotificationAlert('Hi, JPush')->iosNotification('测试', array('sound' => 'sound.caf','badge' => '+1','content-available' => true,'mutable-content' => true,'category' => 'jiguang','extras' => array('key' => 'value'),))->message('测试', array('title' => '测试','content_type' => 'text','extras' => array('key' => 'value'),))->options(array(// sendno: 表示推送序号,纯粹用来作为 API 调用标识,// API 返回时被原样返回,以方便 API 调用方匹配请求与返回// 这里设置为 100 仅作为示例// 'sendno' => 100,// time_to_live: 表示离线消息保留时长(秒),// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。// 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到// 这里设置为 1 仅作为示例// 'time_to_live' => 1,// apns_production: 表示APNs是否生产环境,// True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送生产环境'apns_production' => true,// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,// 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送// 这里设置为 1 仅作为示例// 'big_push_duration' => 1))->send();dump($response);} catch (APIConnectionException $e) {dump($e->getMessage());} catch (APIRequestException $e) {dump($e->getMessage());}}
}

客户端由前端配置无误之后。访问链接即可,成功之后会返回

array(3) {["body"] => array(2) {["sendno"] => string(10) "1609075934"["msg_id"] => string(16) "9007232746737994"}["http_code"] => int(200)["headers"] => array(10) {[0] => string(15) "HTTP/1.1 200 OK"["Server"] => string(5) "nginx"["Date"] => string(29) "Thu, 05 Dec 2019 09:09:46 GMT"["Content-Type"] => string(16) "application/json"["Content-Length"] => string(2) "51"["Connection"] => string(10) "keep-alive"["X-Rate-Limit-Limit"] => string(3) "600"["X-Rate-Limit-Remaining"] => string(3) "599"["X-Rate-Limit-Reset"] => string(2) "60"["X-Jpush-Timestamp"] => string(13) "1575536986356"}
}

注意事项

在开发过程我需要最多的话是报JPush\\ – [1004]: does not exist这个错误,原因的话是选错了Key跟(我是从账户信息进去看到key跟的)。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了