資料連接:
https://www.iteye.com/blog/zxs19861202-1532460
http://www.voidcn.com/search/hxjzwm
<?php namespace notice; use JPush\Exceptions\APIConnectionException; use JPush\Exceptions\APIRequestException; use JPush\Client as JPush; class notice{ /* * ios * */ public function Jpush($app,$data){ $app_key = $app['ios']['app_key']; $master_secret = $app['ios']['master_secret']; $client = new JPush($app_key, $master_secret);
try {
$response = $client->push()
->setPlatform(array('ios'))//, 'android'
// 一般情況下,關於 audience 的設置只需要調用 addAlias、addTag、addTagAnd 或 addRegistrationId
// 這四個方法中的某一個即可,這里僅作為示例,當然全部調用也可以,多項 audience 調用表示其結果的交集
// 即是說一般情況下,下面三個方法和沒有列出的 addTagAnd 一共四個,只適用一個便可滿足大多數的場景需求
// ->addAlias('alias')
->addTag(array('1','2', '3','4'))
//->addRegistrationId($registration_id)
// ->addAllAudience()
->setNotificationAlert('Hi, JPush')
->iosNotification('Hello IOS', array(
'sound' => 'sound.caf',
// 'badge' => '+1',
// 'content-available' => true,
// 'mutable-content' => true,
'category' => 'jiguang',
'extras' => array(
'key' => 'value',
'jiguang'
),
))
->androidNotification('Hello Android', array(
'title' => 'hello jpush',
// 'builder_id' => 2,
'extras' => array(
'key' => 'value',
'jiguang'
),
))
->message('message content', array(
'title' => 'hello jpush',
// 'content_type' => 'text',
'extras' => array(
'key' => 'value',
'jiguang'
),
))
->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' => true,
// 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;
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
print $e;
}
}
/*
* 安卓
* */
public function Fcm($uid,$content,$type,$title){
$url = 'https://fcm.googleapis.com/fcm/send';
$api_key = '';
//准備消息內容
$data = [
'message' => $content, //推送內容
'noticeType' => $type, //推送類型
'title' => $title, //推送標題
];
$fields = array(
//群發使用registration_ids[為數組] 單個用戶使用to
'to' => $uid,
// 'registration_ids' => xxx,
'data' => $data,
// 'time_to_live' => xxx,
);
$headers = array(
'Authorization: key=' . $api_key,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //請求超時時間這里設置為5s
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
if (isset(json_decode($result,true)['multicast_id']) and $result != null){
return ucceeds();die();
}
return Errors();die();
// return $result;
}
}