web-push-php icon indicating copy to clipboard operation
web-push-php copied to clipboard

push notification json undefined with payload

Open binipinki opened this issue 6 years ago • 1 comments

i have this in my php file

$auth = array( 'VAPID' => array( 'subject' => 'mailto:[email protected]', // can be a mailto: or your website address 'publicKey' => 'somekeyyyy', 'privateKey' => 'private_keuuuuuuu', // in the real world, this would be in a secret file ), );

$notifContent = array( 'body' => 'This is the body content', 'icon' => '/img/profile.jpg', );

$pay_load=json_encode($notifContent);

$subscription_new = [ 'subscription' => Subscription::create([ 'endpoint' => $endpoint, 'publicKey' => $publicKey, 'authToken' => $authToken, 'contentEncoding' => $contentEncoding ], true), // 'payload' => $pay_load, ];

$webPush = new WebPush($auth); $res = $webPush->sendNotification( $subscription_new['subscription'], $pay_load, true );

here is serviceworker.js

self.addEventListener('push', function (event) { if (!(self.Notification && self.Notification.permission === 'granted')) { return; }

const sendNotification = body => { const title = "Web Push example"; return self.registration.showNotification(title, { body, }); };

if (event.data) { // const message = event.data.text(); const message = event.data.json(); event.waitUntil(sendNotification(message)); } });

works fine with event.data.text(); but with json does not work and i get [object Object]

payload is not displayed in any case whatever i do i cant send $notifContent data .

i read all open and closed isssue and several said changing .text() to .json() solve the issue. i have seen issue 194 and 202 also but ultimately solution is nowhere. plz help

binipinki avatar Mar 09 '19 11:03 binipinki

use in php json_encode($object) and after in serviceWorker.js

const sendNotification = function(data)  {
      
        let info = JSON.parse(data);   //  this is decode php string

        return self.registration.showNotification( info.title, {
            icon: info.image,
            body: info.description,
        });
    };

developergb19 avatar Apr 04 '19 07:04 developergb19