1

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?

calli23
  • 79
  • 9

1 Answers1

0

You could try a few things, actually:

A. Add contentEncoding to Subscription object:

$sub = Subscription::create([
    'endpoint' => $result->endpoint,
    'publicKey' => $result->publicKey,
    'authToken' => $result->authToken,
    'contentEncoding' => $result->getEncoding(), // from browser, see example link
]);

B. Explicitly set VAPID auth to sendNotification:

$webPush->sendNotification($sub, \json_encode($payload), !$stack, $options, $auth);

Example link for getting the encoding from browser: https://github.com/Minishlink/web-push-php-example/blob/master/src/app.js#L178

Hope that helps.

t1gor
  • 1,244
  • 12
  • 25
  • When I use contentEncoding (e. g. aes128gcm for Google Chrome), the push notification is not shown but in chrome gcm-internals there comes up an issue in Message Decryption Failure Log with error message "The message payload is smaller than the smallest valid message (104 bytes)". But as you can see, a payload is sent. Sending the VAPID with sendNotification does not change anything at all. – calli23 Oct 17 '18 at 12:28