web-push-libs / web-push-php

Web Push library for PHP
MIT License
1.69k stars 295 forks source link

Payload didnt executed #327

Closed mrpotensial closed 3 years ago

mrpotensial commented 3 years ago

sorry if i do this wrong, first time creating issue

Setup

Please provide the following details, the more info you can provide the better.

Please check that you have installed and enabled these PHP extensions :

Please select any browsers that you are experiencing problems with:

Please specify the versions (i.e. Chrome Beta, Firefox Beta etc). Chrome Version 91.0.4472.19 Microsoft Edge Version 90.0.818.46

Problem

im trying to set the tittle and the massage for the notification but didnt executed instead showing as plain text like this image. notif

Expected

i can change the content of notification by changing the serviceworker, but cant change it dynamicly based on payload

Features Used

Example / Reproduce Case

Please provide a code sample that reproduces the issue. If there is a repository that reproduces the issue please put the link here.

Other

im already using json_encode to my array before using them in payload, but stil didnt work, so my code become like this

$report = $webPush->sendOneNotification( $subscription, '{"title":"hello","msg":"yes it works"}' );

Please put any remaining notes here.

tahir1002 commented 3 years ago

Can you share your Service Worker code?

mrpotensial commented 3 years ago

im still using serviceworker code from here

tahir1002 commented 3 years ago
const sendNotification = body => {
    // you could refresh a notification badge here with postMessage API
    const title = "Web Push example";

    return self.registration.showNotification(title, {
        body,
    });
};

Your body object should contain the following properties

{
  "//": "Visual Options",
  "body": "<String> body.msg (in your case)",
  "icon": "<URL String>",
  "image": "<URL String>",
  "badge": "<URL String>",
  "vibrate": "<Array of Integers>",
  "sound": "<URL String>",
  "dir": "<String of 'auto' | 'ltr' | 'rtl'>",

  "//": "Behavioral Options",
  "tag": "<String>",
  "data": "<Anything>",
  "requireInteraction": "<boolean>",
  "renotify": "<Boolean>",
  "silent": "<Boolean>",

  "//": "Both visual & behavioral options",
  "actions": "<Array of Strings>",

  "//": "Information Option. No visual affect.",
  "timestamp": "<Long>"
}
mrpotensial commented 3 years ago

im solving this with converting data in body to array, so my service worker code become like this,

const sendNotification = body => { const title = "Title Here"; var option2 = JSON.parse(body); return self.registration.showNotification(title, option2); };