JPush API client library for PHP

Build Status

JPush API client library for PHP

简要概述

快速安装

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页面有详细的版本发布记录与下载。
相关参数说明文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值