更新

JPush API PHP Client v3.5.0

说明

  • 增加对命名空间的全面支持
  • 使用 PSR-4 标准加载类
  • 建立异常处理体系
  • 更改 API 请求返回格式
  • 不再提供 PushPayload 中"多次调用 addXXX 操作,最终将在多次结果中取并集" 的功能,有这种需求需要开发者自行实现;
  • 重写了 PushPayload 中 addIosNotification() 和 addAndroidNotification() 两个函数,其替代函数为 iosNotification() 和 androidNotification(),其使用数组参数调用起来更加方便;
  • 重写了 PushPayload 中 setMessage() 和 setOptions() 两个函数,其替代函数为 message() 和 options() 其使用数组参数调用起来更加方便;
  • 去掉了发送推送消息时自动指定 sendno 的做法,有这种需求需要开发者自行实现;
  • 去掉 ReportPayload 中各个方法中 "以逗号分隔的 msg_id" 形式的字符串参数;
  • 拆分更新设备的函数为 updateAlias(), addTags(), removeTags(), updateMoblie() 四个使得调用起来更加方便;
  • 拆分更新标签的函数为 addDevicesToTag() 和 removeDevicesFromTag()。

安装

  • 在项目中的 composer.json 文件中添加 jpush 依赖:
"require": {
    "jpush/jpush": "v3.5.*"
}
  • 执行 $ php composer.phar install$ composer install 进行安装。

使用

简单推送给全部设备

use JPush\Client as JPush;
...
...
// JPush SDK 也支持链式调用

    $app_key = 'xxxx';
    $master_secret = 'xxxx';
    $client = new JPush($app_key, $master_secret);
    $pusher = $client->push();
    $pusher->setPlatform('all');
    $pusher->addAllAudience();
    $pusher->setNotificationAlert('Hello, JPush');
    try {
        $response = $pusher->send();
    } catch (\JPush\Exceptions\JPushException $e) {
        // try something else here
        print $e;
    }
...

重要说明

旧版本的 API 请求返回的是一个对象,新版本返回的是一个数组。由于改变了 API 请求的返回格式,所以请求结果和之前是不兼容的,需要相应的做一下转换,不过,还是建议直接处理作为数组的返回结果。

$result = array();
$result['data'] = (object)$response['body'];
$result['limit'] = (object)$response['headers'];
echo 'Result=' . json_encode($result);

源码 Github 链接: https://github.com/jpush/jpush-api-php-client