Minishlink / web-push-php-example

An example for sending Web Push notifications, using web-push-php

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do you set Options ?

Sandbird opened this issue · comments

I've seen some options can be set like 'icon, badge, vibrate etc', but i cant find any way of adding them to the sender. It uses part the serviceworker and part the php...where do you set them ?

Can you exactly tell use your scenario.
I am using it in php and serviceWorker.js file

image

And this is service worker code


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

        const content = body.split('@');
        return self.registration.showNotification(content[0], {
            body: content[1],
            icon: content[2],
            data: {
                click_url: content[3]
            },
            tag: Date.now()
        });
    };

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

I hope you will get it and it will solve your problem

Yeah i was having problems figuring out how to send stuff from php to the worker. Eventually i figured it out. All work fine besides the 'sound' variable. That doesn't work for some reason. I tried everything i could think of. But its a small price to pay.
I did something similar, send everything in the payload then split them up to the options:

<?
$content = array(
	'title' => 'New comment',
	'body' => 'You have a new comment.',
	'icon' => $imagePath.'chat.jpg',
	'badge' => $imagePath.'badge.png',
	'image' => $imagePath.'banner.png',
	'sound' => $soundPath.'notification-sound.mp3',
	'vibrate' => [200, 100, 200, 100, 200, 100, 200],
	'data' => [
		'priority'=>'high',
		'url' => 'https://xxxxx/'
	],
	'actions' => [
		[
		'action' => json_encode(['action'=>'open_url', 'action_url' => 'https://xxxxx/']),
		'title' => 'Open',
		]
	],
	'tag' => 'notify',
	'renotify' => true
);
$pay_load=json_encode($content);
....
$webPush->sendNotification(
    $subscription,
    $pay_load
);
.....

let info = JSON.parse(data);
return self.registration.showNotification( notificationTitle, {
	icon: info.icon,
	badge: info.badge,
	vibrate: info.vibrate,
	image: info.image,
	sound: info.sound,
	tag: info.tag,
	renotify: true,
	data: info.data,
	actions: info.actions,	            
	body: info.body
});

Thanks for taking the time to answer.

What kind of error are you getting?

Nothing, no error...there is just no sound playing. Both on PC and android. Tried local path, and also http type location of the file. Maybe because it's an .mp3? I dont know. But i am getting similar notifications on my PC from other places and they do have sound, but i dont know how and why it works with their worker.

I think you should open another issue with the sound topic and close this up what do you think?

Yeah yeah, dont worry about it. Thank for the help :)