"require": { "jpush/jpush": "^3.5" }
composer 下载 jpush/jpush
引用JPush
// jpush
function jSend($config,$notification, $alias = null, $extra = [], $product = true)
{
try {
$client = new JPush($config['AppKey'], $config['Secret']);
$result = $client->push()
->setPlatform(array('ios', 'android'))
->addAlias($alias)
->setNotificationAlert($notification)
->addAndroidNotification($notification, 'title', 1, $extra)
->addIosNotification($notification, '1', 1, true, 'jiguang',$extra)
->send();
} catch (\Exception $exception) {
trace('提交订单失败:' . $exception->getMessage());
}
}
更全
<?php
use JPush\Client as Client;
class JPush
{
/**
* 通过别名发送极光推送消息
* @param $title // 标题
* @param $content // 内容
* @param $alias // 别名
* @param array $params // 扩展字段
* @param string $ios_badge // ios 角标数
* @param array $platform // 推送设备
* @return array
*/
public static function pushMessageByAlias ($title, $content, $alias, $params = [], $ios_badge = '0', $platform = ['ios', 'android'])
{
if (!is_array ($alias)) return false;
$jpush_conf = Output::getJPushKey (); // 获取配置信息 app_key 和 master_secret
$app_key = $jpush_conf[ 'app_key' ];
$master_secret = $jpush_conf[ 'master_secret' ];
try {
// 初始化
$client = new Client($app_key, $master_secret);
$result = $client->push ()
->setPlatform ($platform)
->addAlias ($alias)
->iosNotification (
$content, [
'sound' => '1',
'badge' => (int)$ios_badge,
'content-available' => true,
'category' => 'jiguang',
'extras' => $params,
])
->androidNotification ($content, [
'title' => $title,
//'build_id' => 2,
'extras' => $params,
])
->options ([
'sendno' => 100,
'time_to_live' => 86400,
'apns_production' => true, // ios推送证书的选择,True 表示推送生产环境,False 表示要推送开发环境
//'big_push_duration' => 10,
])
->send ();
return $result;
} catch (\Exception $e) {
// 日志
}
}
}