Git Product home page Git Product logo

jolinotif's Introduction

JoliNotif demo

Total Downloads Latest Stable Version Latest Unstable Version

About JoliNotif

JoliNotif is a cross-platform PHP library to display desktop notifications. It works on Linux, Windows or macOS.

Requires PHP >= 8.1 (support for PHP 5 was available in version 1.x, for PHP 7.0 and 7.1 in version < 2.1.0, for PHP 7.2 and 7.3 in version < 2.4.0, for PHP < 8.0 in version 2.6.0).

Note

This library can not be used in a web context (FPM or equivalent). Use it in your CLI scripts or in a CRON

Installation

Use Composer to install JoliNotif in your project:

composer require "jolicode/jolinotif"

Usage

include __DIR__.'/vendor/autoload.php';

use Joli\JoliNotif\Notification;
use Joli\JoliNotif\DefaultNotifier;

$notifier = new DefaultNotifier();

// Create your notification
$notification =
    (new Notification())
    ->setTitle('Notification title')
    ->setBody('This is the body of your notification')
    ->setIcon(__DIR__.'/path/to/your/icon.png')
    ->addOption('subtitle', 'This is a subtitle') // Only works on macOS (AppleScriptDriver)
    ->addOption('sound', 'Frog') // Only works on macOS (AppleScriptDriver)
;

// Send it
$notifier->send($notification);

A shell executable is also provided to use JoliNotif from CLI:

jolinotif --title "Hello" --body "World"

Further documentation

Discover more by reading the docs:

You can see the current and past versions using one of the following:

And finally some meta documentation:

Credits

License

JoliNotif is licensed under the MIT License - see the LICENSE file for details.

jolinotif's People

Contributors

carusogabriel avatar gremo avatar javiereguiluz avatar jeremyfreeagent avatar julienbourdeau avatar kamilsk avatar lyrixx avatar matthieumota avatar morawskim avatar nunomaduro avatar paulmallet avatar pborreli avatar peter279k avatar pyrech avatar raphael-da-silva avatar reinfi avatar tamtamchik avatar vesper8 avatar xavierlacot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jolinotif's Issues

problem with Monolog integration

Hi! I found a mistake when writing handler for Monolog based on your useful library:

use Joli\JoliNotif\Notification;
use Joli\JoliNotif\NotifierFactory;

($notifier = NotifierFactory::create()) && $notifier->send(
    (new Notification())
        ->setTitle('Something happen')
        // problem body:
        // ->setBody('[2016-03-15 14:17:21] test.INFO: test [] {"time_execution":"0.000"}')
        // it's ok
        ->setBody('[2016-03-15 14:17:21] test.INFO: test [] {time_execution:0.000}')
);

Problem located here: https://github.com/jolicode/JoliNotif/blob/v1.0.4/src/Notifier/AppleScriptNotifier.php#L56

Possible solution:

$script = 'display notification "'. addslashes($notification->getBody()) .'"';

What do you think? Can I fix it?

Trigger the notification from a webapp

Greate project you got here, I was wondering if its posible to trigger this notifications from a webapp. I tried but the notification doesn't show. If I run the example from the terninal then it shows. Any pointers will be greatly appreciated.
From web
screen shot 2018-06-09 at 21 19 04
From terminal
screen shot 2018-06-09 at 21 19 25
screen shot 2018-06-09 at 21 19 47

Passing a command as string when creating a "Symfony\Component\Process\Process" instance is deprecated since Symfony 4.2

Hello and many thanks for this awesome package,

I'm getting a deprecation warning when testing with phpunit:

Passing a command as string when creating a "Symfony\Component\Process\Process" instance is deprecated since Symfony 4.2, pass it as an array of its arguments instead, or use the "Process::fromShellCommandline()" constructor if you need features provided by the shell.

I solved this problem by changing a line code in Joli\JoliNotif\Util\OsHelper::getMacOSVersion()
from $process = $process = new Process('sw_vers -productVersion');
to $process = new Process(['sw_vers', '-productVersion']);

Add support for Linux subsystem on Windows

This may be a complete edge case, but I needed desktop notifications on the linux subsystem running on windows.

The good news is that the ToastNotifier works fine, but detecting this situation is a little complex it would seem and there are no definitive answers out there.

I couldn't find any information on how to detect the linux subsystem running on windows, but the best thing I could find was:

 $isLinuxOnWindows = mb_strpos(strtolower(php_uname()), 'microsoft') !== false

I have implemented this in my setup by extending the NotifierFactory, and rewriting the methods:

class NotifierFactory extends JoliNotifFactory
{
    /**
     * @return Notifier[]
     */
    public static function getDefaultNotifiers()
    {
        // Don't retrieve notifiers which are certainly not supported on this
        // system. This helps to lower the number of process to run.
        if (OsHelper::isUnix() && !mb_strpos(strtolower(php_uname()), 'microsoft')) {
            return self::getUnixNotifiers();
        }

        return self::getWindowsNotifiers();
    }

    /**
     * @return Notifier[]
     */
    private static function getWindowsNotifiers()
    {
        return [
            new ToasterBashWindowsNotifier(),
            new ToasterNotifier(),
            new NotifuNotifier(),
        ];
    }
}

And then creating a ToasterBashWindowsNotifier that just extends ToasterNotifier:

class ToasterBashWindowsNotifier extends ToasterNotifier
{
    /**
     * {@inheritdoc}
     */
    public function canBeUsed()
    {
        return OsHelper::isUnix() && mb_strpos(strtolower(php_uname()), 'microsoft');
    }
}

and all works now as expected, with notifications popping up as normal.

The only thing I've found is that icons seem to break it at the moment.

I haven't looked in detail, but my guess would be that:

  • Notifier expects a path it can resolve in the current OS, so passing a windows style path means the icon doesn't even try to get displayed when running in Linux (it can't find the file). So e.g. passing C:\myapp\myicon.png doesn't work.
  • Toast expects a path it can resolve in Windows, and passing a Linux path causes it to fail - so passing mnt/c/myapp/myicon.png doesn't work.

I've tracked this down to the 'setIcon' function on the notification, where it does a 'realpath' which turns to null if a windows path is given on the linux subsystem - fuuuuuu.

Icons aren't 100% important to me, so I can forgo supporting them atm.

I did have a thought that Toast might be able to support some sort of base64 encoded png - but I can't get it to work.

Change "SnoreToast"

Hi,

is it possible to change the "SnoreToast"-text within the notifications on windows?

Regards
Philipp

Requirement of psr/log 3.0

Hey,

do your code really depends on "psr/log": "^3.0"? Or would 1.0 and 2.0 also be valid?

Reason:

This blocks your update in a lot of applications because many library packages from frameworks still does not support latest psr/log.

If all psr/log versions would be acceptable I'll be happy to submit a pull request.

Best regards
Martin

The notify() function in a WSL environment

Hello,

I'm in a WSL2 environment under Windows 11 in order to launch my Castor commands and with the Castor.PHAR archive for Windows (since it's this one to take even for WSL). I just tried your notification function but it doesn't trigger any notification.

I have checked my notifications options btw, and everything is properly configured. I wonder the reason behind this : is this a WSL related issue which can't communicate properly with Windows ?

#[AsTask(description: 'Suppression des volumes, conteneurs et réseaux créés pour le projet')]
function clean(bool $force = false)
{
    notify('The infrastructure has been destroyed.');
}

Synology DSM 6.1 Not working

Synology DS416+
Php : 5.6
Apache : 2.6
ALL module php active

message:
No supported notifier

I copied the directory on a mac with a MAMP, everything works, but impossible to run on synology, an idea?

merci

Not working if script run from cron

Ubuntu 14..04
Trying to run file with notifier directly - works.
Trying to run in cron, like:

  • * * * * php /path/notifier.php
    Nothing happens.

root or my own user - not working
log says script is run ok

Can I run it from cron to get result?

Prepare a public release

Todo before a public release:

  • register on packagist
  • make sure the package is installable via composer
  • configure travis and its badge

Notification permission

Is there any way to ask for permission and if user allow only then
we can show notification . right now it is showing all notification by default

Script hangs while notification is visible

I'm using JoliNotif with SnoreNotification on Windows.

When I send a notification, the script then hangs until either the notification is cleared (by clicking it, for example) or the process times-out

Example of a timeout:

   Symfony\Component\Process\Exception\ProcessTimedOutException 

  The process ""W:\app\vendor\jolicode\jolinotif/bin/snoreToast/snoretoast-x86.exe" -m "With Failures" -t "artisan test finished"" exceeded the timeout of 60 seconds.

Using $process->start() instead of $process->run() in the CliBasedNotifier removes the hang.

https://github.com/jolicode/JoliNotif/blob/main/src/Notifier/CliBasedNotifier.php#L88

Is there a reason to use run() instead of start()? (or what about adding an option to specify?)

Not working

I just tried it with the example code and it is showing the Notification successfully sent with Joli\JoliNotif\Notifier\NotifySendNotifier but not working.

Field for answer

Hello.
Please tell me. It is possible to work with additional notification functionality. For example a field for an answer?
image

shell_exec not working

I am trying to use your code (JoliNotif). When I run directly in CMD works fine, but if I try run in shell_exec not working.

Error message:

Fatal error: Uncaught Symfony\Component\Process\Exception\ProcessTimedOutException: The process ""F:\wamp64\www\JoliNotif\vendor\jolicode\jolinotif/bin/snoreToast/snoretoast-x86.exe" -m "This is the body of your notification" -t "Notification title"" exceeded the timeout of 60 seconds. in F:\wamp64\www\JoliNotif\vendor\symfony\process\Process.php on line 1152

Symfony\Component\Process\Exception\ProcessTimedOutException: The process ""F:\wamp64\www\JoliNotif\vendor\jolicode\jolinotif/bin/snoreToast/snoretoast-x86.exe" -m "This is the body of your notification" -t "Notification title"" exceeded the timeout of 60 seconds. in F:\wamp64\www\JoliNotif\vendor\symfony\process\Process.php on line 1152

Call Stack:
    0.0002     392504   1. {main}() F:\wamp64\www\JoliNotif\index.php:0
    0.4557     840440   2. Joli\JoliNotif\Notifier\CliBasedNotifier->send($notification = class Joli\JoliNotif\Notification { private ?string $title = 'Notification title'; private ?string $body = 'This is the body of your notification'; private ?string $icon = ''; private array $options = ['subtitle' => 'This is a subtitle', 'sound' => 'Frog'] }) F:\wamp64\www\JoliNotif\index.php:21
    0.4557     841960   3. Symfony\Component\Process\Process->run($callback = ???, $env = ???) F:\wamp64\www\JoliNotif\vendor\jolicode\jolinotif\src\Notifier\CliBasedNotifier.php:82
    0.4641     867488   4. Symfony\Component\Process\Process->wait($callback = ???) F:\wamp64\www\JoliNotif\vendor\symfony\process\Process.php:249
   60.5834     867488   5. Symfony\Component\Process\Process->checkTimeout() F:\wamp64\www\JoliNotif\vendor\symfony\process\Process.php:423

Can you help me? Whats the problem?

Thanks

Slack

Is it hard to add a Slack notifier?

Icon not shown in desktop notifier

Hi, the desktop notifier works fine, but even adding the code
resource_path('image.png')
the image is not showing. I read some other posts for which it would need required to install other package into the OSX o Windows pc, but this cannot be asked to all users using this feature.

Any solution?

Usage of return hint `self` makes inheritance impossible

On the release v2.0.0 the feature Added typehints everywhere was introduced. I am having an problem extending classes that makes usage of the return hint self. Example:

class MyClass extends \Joli\JoliNotif\Notification
{
...

    public function setTitle(string $title): self
    {
        $this->title = $title;
        return $this;
    }
}

The following Fatal error is throw: Declaration of Joli\JoliNotif\Notification::setTitle(string $title): Joli\JoliNotif\Notification must be compatible with MyClass..

Solution 1: Remove those return hints.

Solution 2: Create a interface and use it on the return hint public function setTitle(string $title): NotificationInterface.

What are your thoughts on this?

icon can`t show in mac os 10.12.3

my mac book pro os is macOS 10.12.3 。� the notification can show ,but the icon can`t display.
I am sure the img is exist and the path is right

Notifier does not resolve on Ubuntu

System

OS: Ubuntu 17.04
PHP: 7.0
JoliNotif: 1.1.1

Details

Notifications do not display

Troubleshooting

If I force $bestNotifier to be NotifySendNotifier (line 37 in Joli\JoliNotif\NotifierFactory

$bestNotifier = new \Joli\JolieNotif\Notifier\NotifySendNotifier;

I do receive a desktop notification

Issue

isSupported() is incorrectly returning false on NotifySendNotifier

In the docs, Advanced usage is referenced but the page is not found

For example, at https://github.com/jolicode/JoliNotif/blob/main/doc/02-notification.md, it mentions "Advanced usage" but that page is not found. https://github.com/jolicode/JoliNotif/blob/main/doc/04-advanced-usage.md

The same applies for other pages thru the help docs

can't get it to work on OSX Mojave

Hi there, just started using https://github.com/spatie/phpunit-watcher which uses this package for the desktop notifications.

I noticed that the notifications are not showing up but can't figure out why.

I am using iTerm by the way on OSX Mojave

I installed jolicode/JoliNotif separately to try to debug the issue

Running ``�`jolinotif --title "Hello" --body "World"```

Does not produce any errors but also no notifications either.

I then tried running the examples/index.php script as suggested in another issue to debug the the problem and the output is simply:

Notification failed with Joli\JoliNotif\Notifier\TerminalNotifierNotifier

Any idea what's going on? Would love to get this working

toast text on Windows 10

Hello.
I see "toast" text on bottom of the popup on Windows 10 with very basic code:

// Create a Notifier
        $notifier = NotifierFactory::create();

        // Create your notification
        $notification =
            (new Notification())
                ->setTitle('Title')
                ->setBody('Done')
                ->setIcon(base_path('chrome.png'))
                ->addOption('subtitle', 'This is a subtitle')// Only works on macOS (AppleScriptNotifier)
                ->addOption('sound', 'Frog') // Only works on macOS (AppleScriptNotifier)
        ;

        // Send it
        $notifier->send($notification);

image

PHP version unconsistent

Your PHP version required (>=5.4.0) doesn't match with the symfony/process one (>=5.5.9 on 3.0).

Create a script to be called directly on CLI

This would allow to use JoliNotif from the CLI, without any PHP code needed. This would allow to run something like:

jolinotif --title="Hello World" --body="This is a notification"

Any opinion about it?

No notification with Windows 10

Windows 10
Mozilla & Chrome
No Internal 500 Error

The code structure looks like the below.

<?php
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL | E_STRICT);
	require_once '.../vendor/autoload.php';
	
	use Joli\JoliNotif\Notification;
	use Joli\JoliNotif\NotifierFactory;

// Create a Notifier
	$notifier = NotifierFactory::create();

// Create your notification
	$notification =
		(new Notification())
			->setTitle('Notification title')
			->setBody('This is the body of your notification')
	;

// Send it
	$notifier->send($notification);

Where is the error?
Thank you.

Update symfony/process dependency

Is it possible to update to also work with symfony/process v5?

I'm having a dependency issue with other projects that depend on JoliNotif, but have installed symfony/process v5.

Try to understand the functionality for future web-projects

Please bare in mind, i'm a self learner,never went to school.
If i run in the terminal: php index.php ( using the example in the README.md)
Notification showsup. PERFECT!

But how i can i let show it up when i visit http://localhost/joli/index.php
For now i get many errors in return, i not blame the code, it must be a misunderstanding of me how i should use it properly.
Would like to learn.
(From what i understood, the visitor must allow notifications via the browser......no?)
screenshot-localhost-2021 10 23-17_12_44

Can't get notifications to work in cron

Hi, I've read the docs on cron, as well as #13.

I've performed all the suggested steps but can't get the notifications working from cron:

* * * * * env DISPLAY=:0 php /home/benjamin/script.php

I verified that the cronjob runs fine. I verified my $DISPLAY:

$ echo $DISPLAY
:0

I've run the suggested xhost command as well:

xhost +local:

But same, no notifications. I'm using Zorin OS 15.3, based on Ubuntu 18.04.

Any idea how to make notifications work in cron?

Nothing Happen

I tried php example/index.php, mac osx yosemite, but no notification pop up.

rtl support

Appreciated your hard work, and it's nice project...

just tested with win 7 working fine.. if possible to add rtl support in setoption method.

or if possible kindly provide some hints to do it.

thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.