JPush API client library for PHP

来源:互联网 发布:秃子套件淘宝怎么找 编辑:程序博客网 时间:2024/06/06 06:09

Build Status

JPush API client library for PHP

简要概述

  • 本API提供简单的接口去调用JPush Push API
  • 本API提供简单的接口去调用JPush Report API
  • 本API提供简单的接口去调用JPush Device API
  • 本API提供简单的接口去调用JPush Schedule API

快速安装

1.复制src/JPush到项目目录下

2.在需要使用JPush的源文件头部 引入 src/JPush/JPush.php 既可使用(注意确认引入的路径是否正确).

# 引入代码require_once("../JPush/JPush.php");

PS: 在下载的中的example文件夹有简单示例代码, 开发者可以参考其中的样例快速了解该库的使用方法.

使用 Composer

如果你的项目使用composer管理依赖, 亦可以通过以下方式使用JPush PHP Library.

  1. composer.json 中添加 jpush依赖, 目前最新版本为 v3.3.9
{    "require":{        "jpush/jpush": "v3.4.2"    }}
  1. 执行 php composer.phar installphp composer.phar update 进行安装

快速使用

代码示例

example文件夹有简单示例代码, 开发者可参考以快速使用该库

examples/├── push_example.php Push API使用示例├── device_example.php Device API使用示例├── report_example.php Report API使用示例└── schedule_example.php Schedule API使用示例

初始化

$client = new JPush($app_key, $master_secret);

简单推送

$result = $client->push()    ->setPlatform('all')    ->addAllAudience()    ->setNotificationAlert('Hi, JPush')    ->send();echo 'Result=' . json_encode($result) . $br;

完整的推送示例

包含指定Platform,指定Alias,Tag,指定iOS,Android notification,指定Message等

$result = $client->push()    ->setPlatform('ios', 'android')    ->addAlias('alias1')    ->addTag(array('tag1', 'tag2'))    ->setNotificationAlert('Hi, JPush')    ->addAndroidNotification('Hi, android notification', 'notification title', 1, array("key1"=>"value1", "key2"=>"value2"))    ->addIosNotification("Hi, iOS notification", 'iOS sound', '+1', true, 'iOS category', array("key1"=>"value1", "key2"=>"value2"))    ->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2"))    ->setOptions(100000, 3600, null, false)    ->send();echo 'Result=' . json_encode($result) . $br;

发送短信推送示例

推送未送达的情况下进行短信送达, 该功能需预付短信费用, 并调用Device API绑定设备与手机号

$result = $client->push()    ->setPlatform('all')    ->addTag('tag1')    ->setNotificationAlert("Hi, JPush SMS")    ->setSmsMessage('Hi, JPush SMS', 60)    ->send();echo 'Result=' . json_encode($result) . $br;

定时推送示例

$payload = $client->push()    ->setPlatform("all")    ->addAllAudience()    ->setNotificationAlert("Hi, 这是一条定时发送的消息")    ->build();// 创建一个2016-12-22 13:45:00触发的定时任务$response = $client->schedule()->createSingleSchedule("每天14点发送的定时任务", $payload, array("time"=>"2016-12-22 13:45:00"));echo 'Result=' . json_encode($response) . $br;

实例代码

use JPush\Client as JPush;/** * @消息推送(最新版本) */function push5($uid,$content,$alias,$live,$type){    $jg = M('System')->where(array('id'=>1))->find();    $app_key = $jg['jg_appkey'];//app_key    $master_secret = $jg['jg_secret'];//master_secre    $client = new JPush($app_key,$master_secret);    try {        $response = $client->push()            ->setPlatform(array('ios', 'android'))            ->addAllAudience()            ->iosNotification($content,array(                    'sound'=>'default',                    'badge' => 2,                    'content-available' => true,                    'category' => 'jiguang',                    'extras' => ['user_id'=>$uid,'alias'=>$alias,'live'=>$live,'type'=>$type]                )            )            ->androidNotification($content,array(                    'title' => $content,                    'builder_id' => 1,                    'extras' => array(                        "user_id"=>$uid,"alias"=>$alias,'live'=>$live,'type'=>$type),                )            )            ->options([                'sendno' => 100,                'time_to_live' => 86400,                'apns_production' => true,                'big_push_duration' => 0            ])            ->send();        return 1;    }catch (APIRequestException $e) {        return 2;    } catch (APIConnectionException $e) {        return 3;    }}

版本更新

Release页面有详细的版本发布记录与下载。
相关参数说明文

原创粉丝点击