ThinkPHP集成极光推送简单实例
准备工作
开始集成
![这里写图片描述](https://img-blog.csdn.net/20170719093357860?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpMzY1NDU2eWlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
配置极光推送
![这里写图片描述](https://img-blog.csdn.net/20170719100844886?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpMzY1NDU2eWlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
'JPUSH' => array(
'APP_KEY' => '',
'MASTER_SECRET' => ''
),
写一个Push控制器
<?php
namespace 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’) |
发送扩展数据
$push->iosNotification('hello', [
'sound' => 'sound',
'badge' => '+1',
'extras' => [
'key' => 'value'
]
]);
$push->androidNotification('hello', [
'sound' => 'sound',
'badge' => '+1',
'extras' => [
'key' => 'value'
]
]);