I am using web-push-php library by Minishlink (https://github.com/web-push-libs/web-push-php) to send push notifications to users. Push notification successfully arrives at the client but the payload is always null.
Here's my code:
serviceWorker.js
self.addEventListener('push', function(event) {
console.log('[SW] push received');
console.log(event.data);
const title = 'Test-Push-Notification';
const options = {
body: 'Yay it works.'/*,
icon: 'images/icon.png',
badge: 'images/badge.png'*/
};
console.log("Notification is about to be shown...");
event.waitUntil(self.registration.showNotification(title, options));
});
sendPushNotification.php
$auth = [
'VAPID' => [
'subject' => 'https://myurl:myport',
'publicKey' => '***',
'privateKey' => '***' // in the real world, this would be in a secret file
],
];
while($result = sqlsrv_fetch_object($getEndpoints)){
$subscription = [
'subscription' => Subscription::create([
'endpoint' => $result->endpoint,
'publicKey' => $result->publicKey,
'authToken' => $result->authToken
], true),
'payload' => '{"msg":"Hello!"}'
];
$webPush = new WebPush($auth);
$res = $webPush->sendNotification(
$subscription['subscription'],
$subscription['payload'],
true
);
}
The subscription data is correctly stored in db. The Push notification arrives with my placeholder text. When I take a look in the console, I see event.data is null. Even when I type console.log(event)
or console.log(event.data.text())
or console.log(event.data.json())
, I don't get any data for data property in that PushMessageData-Object.
Here see my output in chrome console
I think, my keys are correct because the push notification only arrives with valid keys.
Is there anything else, I could check?