ThinkPHP集成极光推送简单实例

来源:互联网 发布:dht网络标示怎么下载 编辑:程序博客网 时间:2024/06/05 22:34

ThinkPHP集成极光推送简单实例

准备工作

  • 极光推送插件下载地 址:https://docs.jiguang.cn/jpush/resources/#sdk_1
  • 极光推送文档地址:https://docs.jiguang.cn/jpush/server/3rd/php_sdk/

开始集成

这里写图片描述

配置极光推送

这里写图片描述

  'JPUSH' => array(        'APP_KEY' => '',        'MASTER_SECRET' => ''    ),

写一个Push控制器

<?phpnamespace Api\Controller;require 'vendor/autoload.php';use JPush\Client as JPushClient;use Think\Controller;class PushController extends Controller{    public function sendAll() {       $client = new JPushClient(C('JPUSH.APP_KEY'),            C('JPUSH.MASTER_SECRET'));        $pusher = $client->push();        //设置发送平台        $pusher->setPlatform('all');        //设置发送对象,发送给所有人        $pusher->addAllAudience();        //设置发送内容        $pusher->setNotificationAlert('Hello, JPush');             try {            $pusher->send();                   } catch (\JPush\Exceptions\JPushException $e) {            print $e;        }    }  }

部分函数讲解

函数名 作用 用法 setPlatform() 设置发送平台 可填写参数有: ios、android、all addAllAudience() 设置发送对象 发送给所有人消息 setNotificationAlert() 设置推送的消息 setNotificationAlert(‘Hello, JPush’); addTag() 发送给某个特定标签的人群 addTag(‘tag1’), addTag(‘tag1’,’tag2’) addRegistrationId() 发送给指定用户id的人,这个id是移动端生成的的 addRegistrationId(‘user1’,’user2’)

发送扩展数据

//ios平台$push->iosNotification('hello', [  'sound' => 'sound',  'badge' => '+1',  'extras' => [    'key' => 'value'  ]]);//安卓平台$push->androidNotification('hello', [  'sound' => 'sound',  'badge' => '+1',  'extras' => [    'key' => 'value'  ]]);
原创粉丝点击