https://www.jianshu.com/p/0ab8c91e30e3按照这个链接配置的
define('AUTH_KEY_PATH', '<path-to-p8-file>');
define('AUTH_KEY_ID', '<Key ID for your p8>');
define('TEAM_ID', '<your team id>');
define('BUNDLE_ID', '<your app bundle identifier>');
private function generateAuthenticationHeader() {
$header = base64_encode(json_encode([
'alg' => 'ES256',
'kid' => AUTH_KEY_ID
]));
$claims = base64_encode(json_encode([
'iss' => TEAM_ID,
'iat' => time()
]));
$pkey = openssl_pkey_get_private('file://' . AUTH_KEY_PATH);
openssl_sign("$header.$claims", $signature, $pkey, 'sha256');
$signed = base64_encode($signature);
return "$header.$claims.$signed";
}
private function sendNotification($debug, $token, $payload) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'apns-topic: ' . BUNDLE_ID,
'authorization: bearer ' . $this->generateAuthenticationHeader(),
'apns-push-type: alert'
]);
// 如果正式环境,$debug=false,不然下面$response会出现BadDeviceToken的错误
$server = $debug ? 'api.development' : 'api';
$url = 'https://'.$server.'.push.apple.com/3/device/'.$token;
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
if ($response === false) {
return ["curl_exec failed: " . curl_error($ch)];
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code === 400 || $code === 410) {
$json = @json_decode($response);
return ['Reason: '.$json->reason, $code];
// if ($json->reason === 'BadDeviceToken') {
// // echo 'BadDeviceToken';
// }
}
curl_close($ch);
return [$response, $code];
}
public function testpush() {
$deviceToken = '<your device token>';
$payload = [
"aps"=>[
"alert"=>[
"title"=>"Game Request",
"body"=>"Bob wants to play poker",
],
"badge"=>1,
],
];
$result = $this->sendNotification(true, $deviceToken, $payload);
echo json_encode($result);
}
["curl_exec failed: Received HTTP/0.9 when not allowed\n"]
0.517144s
[ShowPageTrace]
尝试重新编译curl支持http2
手机能接收消息
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。