web-push-php
web-push-php copied to clipboard
PHP Fatal error: Uncaught InvalidArgumentException: Unable to load the key. in /vendor/web-token/jwt-key-mgmt/KeyConverter/KeyConverter.php:182
Getting PHP Fatal error: Uncaught InvalidArgumentExeption: Unable to load the key.
[Sat Aug 15 03:09:33.197972 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] Stack trace:
[Sat Aug 15 03:09:33.198257 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] #0 /home/wwwserver/vendor/web-token/jwt-key-mgmt/KeyConverter/KeyConverter.php(124): Jose\\Component\\KeyManagement\\KeyConverter\\KeyConverter::loadKeyFromPEM('-----BEGIN EC P...', NULL)
[Sat Aug 15 03:09:33.198527 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] #1 /home/wwwserver/vendor/web-token/jwt-key-mgmt/JWKFactory.php(298): Jose\\Component\\KeyManagement\\KeyConverter\\KeyConverter::loadFromKey('-----BEGIN EC P...', NULL)
[Sat Aug 15 03:09:33.198776 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] #2 /home/wwwserver/vendor/minishlink/web-push/src/VAPID.php(54): Jose\\Component\\KeyManagement\\JWKFactory::createFromKey('-----BEGIN EC P...')
[Sat Aug 15 03:09:33.198962 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] #3 /home/wwwserver/vendor/minishlink/web-push/src/WebPush.php(88): Minishlink\\WebPush\\VAPID::validate(Array)
[Sat Aug 15 03:09:33.199126 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] #4 /home/wwwserver/test/notifyendpoints.php(36): Minishlink\\WebPush\\WebPush->__construct(Array)
[Sat Aug 15 03:09:33.199146 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] #5 {main}
[Sat Aug 15 03:09:33.199316 2020] [:error] [pid 15345:tid 140041997231872] [client xx.xx.xx.xx:37601] thrown in /home/wwwserver/vendor/web-token/jwt-key-mgmt/KeyConverter/KeyConverter.php on line 182
Can you try on v6.0.1 please ?
I have the same problem on v6.0.1. In tmp.json is my data stored that I get from the following
registration.pushManager.subscribe(options).then((subscriptionResponse) => {
// do something with subscriptionResponse...
});
This is my server PHP test file.
$publicKey = trim(file_get_contents('./public_key.txt'));
$privateKey = trim(file_get_contents('./private_key.txt'));
$subscriptionInfo = json_decode(file_get_contents('tmp.json'), true);
$auth = [
'VAPID' => [
'subject' => 'mailto:[email protected]', // can be a mailto: or your website address
'publicKey' => $publicKey, // (recommended) uncompressed public key P-256 encoded in Base64-URL
'privateKey' => $privateKey, // (recommended) in fact the secret multiplier of the private key encoded in Base64-URL
'pemFile' => __DIR__ . '/private_key.pem',
],
];
$notification = [
'subscription' => Subscription::create([
"endpoint" => $subscriptionInfo['endpoint'],
"keys" => [
'p256dh' => $subscriptionInfo["keys"]["p256dh"],
'auth' => $subscriptionInfo["keys"]["auth"]
],
]),
'payload' => null,
];
$webPush = new WebPush($auth);
$webPush->setReuseVAPIDHeaders(true);
$report = $webPush->sendOneNotification(
$notification['subscription'],
$notification['payload'] // optional (defaults null)
);
I was playing around with this last week, and somehow managed to get it working. Think I just removed the 'pemFile' entry from the VAPID auth array.
Not sure how this solved it or if there will be problems going forward, but for now it is working. Been getting my notifications properly since.
@emerysteele Thanks for your help. I don't know, if this workaround triggers some unexpected behavoir that we don't see yet, but for now it works.