从https://docs.jiguang.cn/jpush/server/sdk/php_sdk/极光官网上下推送的demo很杂,需要精简下,
搞定之后放在vendor文件夹下,目前可以开始第一步,先测试全部消息推送,这个可以直接在下载的极光demo里直接用
public function push()
{
vendor("JPush.Client");
vendor("JPush.Config");
vendor("JPush.PushPayload");
vendor("JPush.Http");
$app_key = '********';//填入自己的key
$master_secret = '********';//填入自己的secret
$client =new \JPush\Client($app_key, $master_secret);
$push_payload = $client->push()
->setPlatform('all')
->addAllAudience()
->setNotificationAlert('Hi, JPush');
try {
$response = $push_payload->send();
print_r($response);
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
print $e;
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
print $e;
}
}
方法里前四个引入是需要的,要不然报错,测试没问题了,开始搞安卓苹果
安卓推送
public function androidpushbyid($data)
{
vendor("JPush.Client");
vendor("JPush.Config");
vendor("JPush.PushPayload");
vendor("JPush.Http");
$app_key = '********';
$master_secret = '*********';
$client =new \JPush\Client($app_key, $master_secret);
try {
$payload = $client->push()
->setPlatform(array('android'))
->addRegistrationId($data)
->setNotificationAlert('Hi, JPush')
->androidNotification('填写你需要的内容', array(
'title' => '填写你需要的内容',
// ---------------------------------------------------
'extras' => array(
'key' => value; //可以让前段识别这个推送是干啥的
),
))
->send();
return json($payload);
} catch (\JPush\Exceptions\APIConnectionException $e) {
print $e;
} catch (\JPush\Exceptions\APIRequestException $e) {
print $e;
}
}
其中方法中的$data这个变量是前端传过来的当前设备唯一的RegistrationId,这个前端可以获取到,拿到后也可以自己操作
苹果推送
public function iospushbyid($data)
{
vendor(“JPush.Client”);
vendor(“JPush.Config”);
vendor(“JPush.PushPayload”);
vendor(“JPush.Http”);
$app_key = ‘*********’;
$master_secret = ‘************’;
c
l
i
e
n
t
=
n
e
w
J
P
u
s
h
(
client = new JPush(
client=newJPush(app_key, $master_secret);
// 完整的推送示例,包含指定Platform,指定Alias,Tag,指定iOS,Android notification,指定Message等
$result =
c
l
i
e
n
t
−
>
p
u
s
h
(
)
−
>
s
e
t
P
l
a
t
f
o
r
m
(
a
r
r
a
y
(
′
i
o
s
′
)
)
−
>
a
d
d
R
e
g
i
s
t
r
a
t
i
o
n
I
d
(
client->push() ->setPlatform(array('ios')) ->addRegistrationId(
client−>push()−>setPlatform(array(′ios′))−>addRegistrationId(data)
->iosNotification(‘填写需要的内容’, array(
‘badge’=>1,//这个是推送的消息显示的未查看的条数
‘title’ => ‘填写需要的内容’,
‘extras’ => array(
‘key’ =>value ,
),
));
try {
$response =
r
e
s
u
l
t
−
>
s
e
n
d
(
)
;
r
e
t
u
r
n
j
s
o
n
(
result->send(); return json(
result−>send();returnjson(response);
}catch (\JPush\Exceptions\APIConnectionException $e) {
print $e;
}catch(\JPush\Exceptions\APIRequestException $e) {
print $e;
}
}
以上就是我整理的极光推送的代码和心得