PHP激光推送,移动端消息通知
composer require jpush/jpush
以下是整理好的方式:
/**
* 极光推送
* 过滤 html 和特殊字符,主要是针对富文本编辑器里产生的HTML和特殊字符
* @param $model array
* @return true
*/
public function msg_push($model)
{
$code = $model['code']; //用户别名
$img = $model['img']; //图片
$msg_id = $model['msg_id']; //消息编号
$extras = [];
$extras['path'] = $model['path']; //类型
$extras['id'] = $model['id']; //ID
$extras['title'] = $model['title']; //标题
$alert = [
'body' => $model['desc'], //内容
'title' => $model['title'], //标题
];
$client = new Client(\Yii::$app->params['push']['AppKey'], \Yii::$app->params['push']['Secret']);
$res = $client->push()
->setPlatform('all')
->addAlias($code)
->iosNotification($alert,[
'sound' => 'sound',
'badge' => '+1',
'content-available' => true,
'category' => 'jiguang',
'extras' => $extras,
])
->androidNotification($desc,[
'title' => $title,
'large_icon' => $img,
'extras' => $extras
])
->message('Hello JPush', [
'title' => 'Hello',
'content_type' => 'text',
'extras' => [
'key' => 'value'
]
])
->options([
'sendno' =>$msg_id,
'apns_production' => true, //环境 false 开发环境(线下) true 生成环境(线上)
]
);
try { //捕获异常,无法获取用户别名不发消息
$res->send();
} finally {
return true;
}
}
以上就是激光推送封装的方法,以及项目中遇到问题总结
如有其他问题请加群讨论