推送通知与PHP“提供了无效的JWT”错误

时间:2019-09-18 01:06:04

标签: php curl jwt push

我正在尝试将推送通知发送到pushSubscription端点,但它不起作用。您在我的代码中看到任何错误吗? 感谢您提前得到我的帮助。我真的希望你们能提供帮助。我在这里撞墙了

公钥+私钥是随机生成的,不在生产中!

// Create token header as a JSON string
$header = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);

// Create token payload as a JSON string
$payload = json_encode(['aud' => 'https://fcm.googleapis.com', 'exp' => time() + 72000, 'sub' => 'mailto:webpush@example.com']);

// Encode Header to Base64Url String
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));

// Encode Payload to Base64Url String
$base64UrlPayload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload));



$public_key = 'BA-BIKtl82vO9x_T3BhBehW6ee_eLCruRNzB9hy9ltNucASORFDnewmVrEKsdmT3W_7bX87pXtWJhKYgPYBruOQ';
$private_key = 'REDACTED';




// Create Signature Hash
$signature = hash_hmac('sha256', $base64UrlHeader . "." . $base64UrlPayload, $private_key);

// Encode Signature to Base64Url String
$base64UrlSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));

// Create JWT
$jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $base64UrlSignature;








/////SEND PUSH//////////

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.fcm.googeapis.com/fcm/send/.....');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = [];
$headers[] = 'Ttl: 60';
$headers[] = 'Content-Length: 0';
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$jwt.'';
$headers[] = 'Crypto-Key: p256ecdsa='.$public_key.'';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

curl_close($ch);

0 个答案:

没有答案