love_php/app/Services/JpushService.php

103 lines
3.4 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Services;
use JPush\Client;
use Log;
class JpushService
{
/**
* 初始化 JPushClient
* @param $app
* @return Client
*/
public static function newJpushClient($app)
{
if ($app == 'android') {
$appKey = config('Jpush.android_app_key');
$master = config('Jpush.android_master_secret');
} elseif ($app == 'ios') {
$appKey = config('Jpush.ios_app_key');
$master = config('Jpush.ios_master_secret');
} else {
return 'APP类型错误';
}
$log_path = config('Jpush.log_file');
$client = new Client($appKey, $master, $log_path);
return $client;
}
/**
* android ios 通过 别名 给单个设备或多个设备推送消息
* @param $params
* @return array
*/
public static function androidOrIosPushByAlias($params)
{
// 推送平台
$platform = array_get($params, 'platform');
// 推送标题
$title = array_get($params, 'title');
// 推送内容
$content = array_get($params, 'content');
// 通知栏样式 ID
$builderId = array_get($params, 'builderId');
// 附加字段 (可用于给前端返回进行其他业务操作例如返回orderId用于点击通知后跳转到订单详情页面)
$extras = array_get($params, 'extras');
// 推送目标 (别名)
$alias = array_get($params, 'alias');
// 唤醒APP路径
$intent = array_get($params, 'intent');
$uri_activity = array_get($params, 'uri_activity');
$uri_action = array_get($params, 'uri_action');
// 推送目标 (注册ID)
$registrationId = array_get($params, 'registrationId');
// 推送类型 (1-别名 2-注册id 3-全部(ios 或 android))
$type = array_get($params, 'type');
// 返回一个推送 Payload 构建器
$push = self::newJpushClient($platform)->push();
$push->setPlatform($platform);
switch ($type) {
// 通过别名推送
case 1:
$push->addAlias($alias);
break;
// 通过注册 ID 推送
case 2:
$push->addRegistrationId($registrationId);
break;
// 推送全部(android 或 ios)
case 3:
$push->addAllAudience();
break;
}
$push->androidNotification($content, [ // android 通知
"title" => $title,
"builder_id" => $builderId,
"extras" => $extras,
"intent" => $intent,
"uri_activity" => $uri_activity,
"uri_action" => $uri_action,
])->iosNotification($content, [ // ios 通知
"sound" => "sound", // 通知提示声音,如果无此字段,则此消息无声音提示;
"badge" => "+1", // 应用角标APP右上角的数字0 清除 默认 +1
"extras" => $extras,
])->options([ // 推送参数
'apns_production' => config('Jpush.environment') // APNs 是否生产环境 (ios)
]);
$response = $push->send();
if ($response['http_code'] != 200) {
// compact('response', 'type', 'platform', 'alias', 'registrationId', 'title', 'content')
// );
}
return $response;
}
}