Recommend this page to a friend! |
Classes of james doe | Web Push Notifications PHP Sender | README.md | Download |
|
DownloadWebPush> Web Push library for PHP WebPush can be used to send notifications to endpoints which server delivers Web Push notifications as described in the Web Push protocol. As it is standardized, you don't have to worry about what server type it relies on. Requirements
PHP 7.1+ is recommended for better performance. For PHP 5.6 or HHVM compatibility, use the v1.x releases. InstallationUse composer to download and install the library and its dependencies.
Usage
Full examples of Web Push implementations
Authentication (VAPID)Browsers need to verify your identity. A standard called VAPID can authenticate you for all browsers. You'll need to create and provide a public and private key for your server. These keys must be safely stored and should not change. You can specify your authentication details when instantiating WebPush. The keys can be passed directly (recommended), or you can load a PEM file or its content:
In order to generate the uncompressed public and secret key, encoded in Base64, enter the following in your Linux bash:
If you can't access a Linux bash, you can print the output of the
On the client-side, don't forget to subscribe with the VAPID public key as the
Notifications and default optionsEach notification can have a specific Time To Live, urgency, and topic.
You can change the default options with
TTLTime To Live (TTL, in seconds) is how long a push message is retained by the push service (eg. Mozilla) in case the user browser is not yet accessible (eg. is not connected). You may want to use a very long time for important notifications. The default TTL is 4 weeks. However, if you send multiple nonessential notifications, set a TTL of 0: the push notification will be delivered only if the user is currently connected. For other cases, you should use a minimum of one day if your users have multiple time zones, and if they don't several hours will suffice. urgencyUrgency can be either "very-low", "low", "normal", or "high". If the browser vendor has implemented this feature, it will save battery life on mobile devices (cf. protocol). topicSimilar to the old batchSizeIf you send tens of thousands notifications at a time, you may get memory overflows due to how endpoints are called in Guzzle.
In order to fix this, WebPush sends notifications in batches. The default size is 1000. Depending on your server configuration (memory), you may want
to decrease this number. Do this while instanciating WebPush or calling Server errorsYou can see what the browser vendor's server sends back in case it encountered an error (push subscription expiration, wrong parameters...).
Firefox errors are listed in the autopush documentation. Payload length, security, and performancePayloads are encrypted by the library. The maximum payload length is theoretically 4078 bytes (or ASCII characters). For compatibility reasons though, your payload should be less than 3052 bytes long. The library pads the payload by default. This is more secure but it decreases performance for both your server and your user's device. Why is it more secure?When you encrypt a string of a certain length, the resulting string will always have the same length, no matter how many times you encrypt the initial string. This can make attackers guess the content of the payload. In order to circumvent this, this library adds some null padding to the initial payload, so that all the input of the encryption process will have the same length. This way, all the output of the encryption process will also have the same length and attackers won't be able to guess the content of your payload. Why does it decrease performance?Encrypting more bytes takes more runtime on your server, and also slows down the user's device with decryption. Moreover, sending and receiving the packet will take more time. It's also not very friendly with users who have limited data plans. How can I disable or customize automatic padding?You can customize automatic padding in order to better fit your needs. Here are some ideas of settings:
* (default)
Customizing the HTTP clientWebPush uses Guzzle. It will use the most appropriate client it finds,
and most of the time it will be You can customize the default request options and timeout when instantiating WebPush:
Common questionsIs there any plugin/bundle/extension for my favorite PHP framework?The following are available:
Feel free to add your own! Is the API stable?Not until the Push API spec is finished. What about security?Payload is encrypted according to the Message Encryption for Web Push standard, using the user public key and authentication secret that you can get by following the Web Push API specification. Internally, WebPush uses the phpecc Elliptic Curve Cryptography library to create
local public and private keys and compute the shared secret.
Then, if you have a PHP >= 7.1, WebPush uses How do I scale?Here are some ideas:
How to solve "SSL certificate problem: unable to get local issuer certificate"?Your installation lacks some certificates.
You can also force using a client without peer verification. How to solve "Bad key encryption key length" or "Unsupported key type"?Disable How to solve "Class 'Minishlink\WebPush\WebPush' not found"Make sure to require Composer's autoloader.
I must use PHP 5.4 or 5.5. What can I do?You won't be able to send any payload, so you'll only be able to use I lost my VAPID keys!See issue #58. I'm using Firebase push notifications, how do I use this library?This library is not designed for Firebase push notifications. You can still use it for your web projects (for standard WebPush notifications), but you should forget any link to Firebase while using the library. I need to send notifications to native apps. (eg. APNS for iOS)WebPush is for web apps. You need something like RMSPushNotificationsBundle (Symfony). This is PHP... I need Javascript!This library was inspired by the Node.js marco-c/web-push library. ContributingSee CONTRIBUTING.md. LicenseSponsorsThanks to JetBrains for supporting the project through sponsoring some All Products Packs within their Free Open Source License program. |