|
| 1 | +# WebPush |
| 2 | +> Web Push library for PHP |
| 3 | +
|
| 4 | +## Installation |
| 5 | +`composer require minishlink/web-push` |
| 6 | + |
| 7 | +## Usage |
| 8 | +WebPush can be used to send notifications to endpoints which server delivers web push notifications as described in |
| 9 | +the [Web Push API specification](http://www.w3.org/TR/push-api/). |
| 10 | +As it is standardized, you don't have to worry about what server type it relies on. |
| 11 | +__*Currently, WebPush doesn't support payloads at all. |
| 12 | +It is under development (see ["payload" branch](https://github.com/Minishlink/web-push/tree/payload).*__ |
| 13 | + |
| 14 | +```php |
| 15 | +<?php |
| 16 | + |
| 17 | +use Minishlink\WebPush\WebPush; |
| 18 | + |
| 19 | +// array of endpoints |
| 20 | +$endpoints = array( |
| 21 | + 'https://android.googleapis.com/gcm/send/abcdef...', // Chrome |
| 22 | + 'https://updates.push.services.mozilla.com/push/adcdef...', // Firefox 43+ |
| 23 | + 'https://example.com/other/endpoint/of/another/vendor/abcdef...', |
| 24 | +); |
| 25 | + |
| 26 | +$webPush = new WebPush(); |
| 27 | +$webPush->sendNotification($endpoints[0]); // send one notification |
| 28 | +$webPush->sendNotifications($endpoints); // send multiple notifications |
| 29 | +``` |
| 30 | + |
| 31 | +### GCM servers notes (Chrome) |
| 32 | +For compatibility reasons, this library detects if the server is a GCM server and appropriately sends the notification. |
| 33 | +GCM servers don't support encrypted payloads yet so WebPush will skip the payload. |
| 34 | +If you still want to show that payload on your notification, you should get that data on client-side from your server |
| 35 | +where you will have to store somewhere the history of notifications. |
| 36 | + |
| 37 | +You will need to specify your GCM api key when instantiating WebPush: |
| 38 | +```php |
| 39 | +<?php |
| 40 | + |
| 41 | +use Minishlink\WebPush\WebPush; |
| 42 | + |
| 43 | +$endpoint = 'https://android.googleapis.com/gcm/send/abcdef...'; // Chrome |
| 44 | +$apiKeys = array( |
| 45 | + 'GCM' => 'MY_GCM_API_KEY', |
| 46 | +); |
| 47 | + |
| 48 | +$webPush = new WebPush($apiKeys); |
| 49 | +$webPush->sendNotification($endpoints[0]); // send one notification |
| 50 | +$webPush->sendNotifications($endpoints); // send multiple notifications |
| 51 | +``` |
| 52 | + |
| 53 | +### Changing the browser client |
| 54 | +By default, WebPush will use `MultiCurl`, allowing to send multiple notifications in parallel. |
| 55 | +You can change the client to any client extending `\Buzz\Client\AbstractClient`. |
| 56 | +Timeout is configurable in the constructor. |
| 57 | + |
| 58 | +```php |
| 59 | +<?php |
| 60 | + |
| 61 | +use Minishlink\WebPush\WebPush; |
| 62 | + |
| 63 | +$webPush = new WebPush(array(), null, null, $client); |
| 64 | +``` |
| 65 | + |
| 66 | +You have access to the inner browser if you want to configure it further. |
| 67 | +```php |
| 68 | +<?php |
| 69 | + |
| 70 | +use Minishlink\WebPush\WebPush; |
| 71 | + |
| 72 | +$webPush = new WebPush(); |
| 73 | + |
| 74 | +/** @var $browser \Buzz\Browser */ |
| 75 | +$browser = $webPush->getBrowser(); |
| 76 | +``` |
| 77 | + |
| 78 | +## Common questions |
| 79 | + |
| 80 | +### Is the API stable? |
| 81 | +Not until the [Push API spec](http://www.w3.org/TR/push-api/) is finished. |
| 82 | + |
| 83 | +### What about security? |
| 84 | +Internally, WebPush uses the [phpecc](https://github.com/phpecc/phpecc) Elliptic Curve Cryptography library. |
| 85 | + |
| 86 | +### How to solve "SSL certificate problem: unable to get local issuer certificate" ? |
| 87 | +Your installation lacks some certificates. |
| 88 | + |
| 89 | +1. Download [cacert.pem](http://curl.haxx.se/ca/cacert.pem). |
| 90 | +2. Edit your `php.ini`: after `[curl]`, type `curl.cainfo = /path/to/cacert.pem`. |
| 91 | + |
| 92 | +You can also force using a client without peer verification. |
| 93 | + |
| 94 | +### I need to send notifications to native apps. (eg. APNS for iOS) |
| 95 | +WebPush is for web apps. |
| 96 | +You need something like [RMSPushNotificationsBundle](https://github.com/richsage/RMSPushNotificationsBundle) (Symfony). |
| 97 | + |
| 98 | +### This is PHP... I need Javascript! |
| 99 | +This library was inspired by the Node.js [marco-c/web-push](https://github.com/marco-c/web-push) library. |
| 100 | + |
| 101 | +## Contributing |
| 102 | +See [CONTRIBUTING.md](https://github.com/Minishlink/web-push/blob/master/CONTRIBUTING.md). |
| 103 | + |
| 104 | +## Tests |
| 105 | +Copy `phpunit.xml` from `phpunit.dist.xml` and fill it with your test endpoints and private keys. |
| 106 | + |
| 107 | +## License |
| 108 | +[MIT](https://github.com/Minishlink/web-push/blob/master/LICENSE) |
0 commit comments