Files
mier-php/application/admin/model/Jpush.php
2025-08-11 10:22:05 +08:00

272 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\model;
use think\Model;
class Jpush extends Model
{
public $client;
public $message_data;
public function __construct()
{
$config = get_system_config();
$this->client = new \JPush\Client($config['jpush_app_key'], $config['jpush_master_secret']);
$this->message_data[1000] = '系统消息';
$this->message_data[1001] = '情缘消息';
$this->message_data[1002] = '陪玩订单消息';
}
//全体推送
public function push_all_notification_message($platform, $title, $message, $data = [])
{
if (empty($this->message_data[$data['code']])) {
return ['code' => 201, 'msg' => '消息类型不存在', 'data' => null];
}
// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
try {
$response = $this->client->push();
if ($platform == 1) {
$response = $response->setPlatform(array('ios'));
} elseif ($platform == 2) {
$response = $response->setPlatform(array('android'));
} elseif ($platform == 3) {
$response = $response->setPlatform(array('ios', 'android'));
}
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
// ->addAlias('alias')
// ->addTag(array('tag1', 'tag2'))
// ->addRegistrationId($registration_id)
$response = $response->addAllAudience()
->iosNotification($message, array(
'title' => $title,
//'sound' => 'sound.caf',
// 'badge' => '+1',
// 'content-available' => true,
// 'mutable-content' => true,
//'category' => '',
'extras' => $data,
))
->androidNotification($message, array(
'title' => $title,
// 'builder_id' => 2,
'extras' => $data
))
->options(array(
// sendno: 表示推送序号,纯粹用来作为 API 调用标识,
// API 返回时被原样返回,以方便 API 调用方匹配请求与返回
// 这里设置为 100 仅作为示例
// 'sendno' => 100,
// time_to_live: 表示离线消息保留时长(秒)
// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
// 默认 86400 1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
// 这里设置为 1 仅作为示例
// 'time_to_live' => 1,
// apns_production: 表示APNs是否生产环境
// True 表示推送生产环境False 表示要推送开发环境;如果不指定则默认为推送开发环境
'apns_production' => false,
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
// 给定的 n 分钟内均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
// 这里设置为 1 仅作为示例
// 'big_push_duration' => 1
))
->setSmsMessage(array(
'delay_time' => 60,
'signid' => 154,
'temp_id' => 1,
'temp_para' => array(
'code' => 357
),
'active_filter' => false
))
->send();
//print_r($response);
return ['code' => 200, 'msg' => '推送成功', 'data' => null];
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
//print $e;
return ['code' => 201, 'msg' => '推送失败', 'data' => null];
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
// print $e;
return ['code' => 201, 'msg' => '推送失败', 'data' => null];
}
}
//个人推送
public function push_notification_message($uid, $title, $message, $data = [])
{
if (empty($this->message_data[$data['code']])) {
return ['code' => 201, 'msg' => '消息类型不存在', 'data' => null];
}
$uid = strval($uid);
// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
try {
$response = $this->client->push()
->setPlatform(array('ios', 'android'));
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
// ->addAlias('alias')
// ->addTag(array('tag1', 'tag2'))
// ->addRegistrationId($registration_id)
//->addAllAudience()
$response = $response->addAlias($uid)
->setNotificationAlert($message)
->iosNotification($message, array(
'sound' => 'sound.caf',
// 'badge' => '+1',
// 'content-available' => true,
// 'mutable-content' => true,
'category' => 'jiguang',
'extras' => $data,
))
->androidNotification($message, array(
'title' => $title,
// 'builder_id' => 2,
'extras' => $data,
))
->message($message, array(
'title' => $title,
// 'content_type' => 'text',
'extras' => $data,
))->options(array(
// sendno: 表示推送序号,纯粹用来作为 API 调用标识,
// API 返回时被原样返回,以方便 API 调用方匹配请求与返回
// 这里设置为 100 仅作为示例
// 'sendno' => 100,
// time_to_live: 表示离线消息保留时长(秒)
// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
// 默认 86400 1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
// 这里设置为 1 仅作为示例
// 'time_to_live' => 1,
// apns_production: 表示APNs是否生产环境
// True 表示推送生产环境False 表示要推送开发环境;如果不指定则默认为推送开发环境
'apns_production' => false,
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
// 给定的 n 分钟内均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
// 这里设置为 1 仅作为示例
// 'big_push_duration' => 1
))
->setSmsMessage(array(
'delay_time' => 60,
'signid' => 154,
'temp_id' => 1,
'temp_para' => array(
'code' => 357
),
'active_filter' => false
))
->send();
print_r($response);
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
//print $e;
return ['code' => 201, 'msg' => '推送失败', 'data' => null];
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
// print $e;
return ['code' => 201, 'msg' => '推送失败', 'data' => null];
}
}
//推送APP内消息
public function push_message($uid, $title, $message, $data = [])
{
if (empty($this->message_data[$data['code']])) {
return ['code' => 201, 'msg' => '消息类型不存在', 'data' => null];
}
// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
try {
$response = $this->client->push()
->setPlatform(array('ios', 'android'));
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
// ->addAlias('alias')
// ->addTag(array('tag1', 'tag2'))
// ->addRegistrationId($registration_id)
//->addAllAudience()
$response = $response->addAlias($uid)
->message($message, array(
'title' => $title,
// 'content_type' => 'text',
'extras' => $data,
))
->options(array(
// sendno: 表示推送序号,纯粹用来作为 API 调用标识,
// API 返回时被原样返回,以方便 API 调用方匹配请求与返回
// 这里设置为 100 仅作为示例
// 'sendno' => 100,
// time_to_live: 表示离线消息保留时长(秒)
// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
// 默认 86400 1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
// 这里设置为 1 仅作为示例
// 'time_to_live' => 1,
// apns_production: 表示APNs是否生产环境
// True 表示推送生产环境False 表示要推送开发环境;如果不指定则默认为推送开发环境
'apns_production' => false,
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
// 给定的 n 分钟内均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
// 这里设置为 1 仅作为示例
// 'big_push_duration' => 1
))
->setSmsMessage(array(
'delay_time' => 60,
'signid' => 154,
'temp_id' => 1,
'temp_para' => array(
'code' => 357
),
'active_filter' => false
))
->send();
print_r($response);
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
//print $e;
return ['code' => 201, 'msg' => '推送失败', 'data' => null];
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
// print $e;
return ['code' => 201, 'msg' => '推送失败', 'data' => null];
}
}
}