A PHP command line to send Push Notifications

app console command 2

In my last post I showed how to write  a simple PHP code to send iOS  push notification using the HTTP2 protocol.

Now we will refactor that code and build a simple command line tool that we can launch from the terminal, like:

This will send the message “ciao” to the device with token “dbdaeae86abcdea6e7dc7b2cbf074c1c389e” (note that this is a dummy token).

SendPush Class

Starting from the PHP code in the previous post, we build a php class wrapping the entire functionality:

This class, that has the public methods: openConnection(), sendPush() and closeConnection(), hides all the complexity of sending the push inside the private method sendHTTP2Push().

The App Structure

Now we build a PHP App  contained inside a folder (named “sendPush”).

To build the app, together with all the libraries we need, we use Composer. This is the PHP dependency manager used by many PHP developer (if you don’t know it, it’s time to read). If you don’t have composer on your computer install it (here the doc).

Then create an empty folder named “sendPush” and enter it from the terminal (“cd sendPush”).

Inside this folder create a file “composer.json” with this content:

(Now and in the following, you can substitute “Tabasoft” with the name of your company (or you), this is just the vendor name; though remember that this name must be the same of that in the namespace of the SendPush class above).

This file says to composer which libraries/code we need for our app.

We require “symfony/console” (that is the library that helps us to build the command line, more below).

The instruction  “autoload” says to the Composer to include all our code from the folder “src/Tabasoft” (recursively).

Now, in the root of the folder “sendPush” digit:

This will install the libraries in a folder called “vendor”.

Now create the folder “src/Tabasoft” (remember to substitute Tabasoft if you want) and put the file “SendPush.php” inside it (this is the file containing the SendPush class previously defined.

At this point this is the structure of our sendPush folder:

app structure

Symfony Console component

The symfony/console component is a library that helps us to build a command line tool from some php code.

It is based ont the concept of Command. A Command is a PHP class that can be execute from terminal. The class must have two methods:

In the configure() method we describe the name of the command, a description, the arguments and options we accept as inputs.

In the execute() method we actually process the command.

In the case of our push notification tool, the command class is:

Note that we accept arguments: “message”, “to” and options “mode”, “app”, “cert” (and we set the default for these inputs).

Save the class in a file “SendPushCommand.php” and put it in the src/Tabasoft folder.

Then type from terminal:

(this causes composer to know which file in src must be autoloaded; it must be executed every time we add/remove files from that folder).

The command line file

The last step is to create the tool file. This is the “main” of our app and its name will be the name of the command typed from terminal.

Create a file “pushsender” in the root of our “sendPush” folder with the following content:

The first line says to the terminal that this file contains PHP code (it must be processed by the command “php”).  The require command says to composer to load all our src files.

Then we create an Application object (Symfony\Component\Console\Application) and add our command to it. We could also add other commands to the same Application.

Then we run the application.

The permission steps

To execute as a terminal script the pushsender file must have the correct file permissions, thus type from the terminal:

Then if we want to call the command simply typing “pushsender” we must add its parent folder path to the PATH system variable. Or alternatively we can create a sym link to it in a “canonical” folder as:

Try it

The Console component provides some features out of the box. Try:

Remember? “send” is the name we gave to our command (SendPushCommand line …->setName(‘send’)…).

app console command 2

Here we have a description of what our command send does and all the argument and options it accepts.

Try for example:

or

symfony error

Conclusions

In this post we demonstrated how to use Symfony Console to build a simple command line from our PHP code. Now we have a terminal tool to send push notification, together with help and description directly readable from the terminal.

Enjoy and read the complete source code.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Valerio Ferrucci

Valerio Ferrucci (valfer) develops software on Apple Macintosh since the 1990s until today for MacOS, OSX and, since some years, iOS. He is also a Web (PHP/MySQL/JS/CSS) and Android Developer.

More Posts - Website

Follow Me:
TwitterFacebookLinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *