|
| 1 | +# phpFCMv1 |
| 2 | + |
| 3 | +phpFCMv1 is an PHP implementation of [FCM](https://firebase.google.com/docs/cloud-messaging) HTTP v1 API |
| 4 | + |
| 5 | +### What is different compared to others FCM Libraries? |
| 6 | +Most of other libraries are implementation of FCM's [Legacy HTTP Server Protocol](https://firebase.google.com/docs/cloud-messaging/http-server-ref). It requires a server key from Firebase console (which means you have to copy and paste in your code) ([Docs](https://firebase.google.com/docs/cloud-messaging/auth-server#authorize_legacy_protocol_send_requests)) |
| 7 | + |
| 8 | +HTTP v1 API, in contrast, leverages OAuth2 security model. You need to get an access token (which is valid for about an hour) in order to request sending notification with service account's private key file. Although |
| 9 | +(See the blog [post](https://firebase.googleblog.com/2017/11/whats-new-with-fcm-customizing-messages.html) about HTTP v1 API) |
| 10 | + |
| 11 | +### References |
| 12 | +* [google/node-gtoken](https://github.com/google/node-gtoken) |
| 13 | +* [google/google-auth-library-nodejs](https://github.com/google/google-auth-library-nodejs) |
| 14 | + : Above two libraries let me understand how HTTP v1 API works in FCM |
| 15 | +* [guzzlehttp/guzzle](https://github.com/guzzle/guzzle) : GuzzleHttp let this library to PSR7 compatible |
| 16 | +* [Paragraph1/php-fcm](https://github.com/Paragraph1/php-fcm) : Inspired me how FCM libraries are used in Legacy HTTP Protocol |
| 17 | + |
| 18 | +### How to use |
| 19 | + |
| 20 | +* Install the library with composer |
| 21 | + |
| 22 | + ``` |
| 23 | + composer install lkaybob/phpFCMv1 |
| 24 | + ``` |
| 25 | + |
| 26 | +* Import the library with *autoload.php* |
| 27 | + |
| 28 | + ```php |
| 29 | + require_once __DIR__ . '/vender/autoload.php'; |
| 30 | + |
| 31 | + use lkaybob\phpFCMv1\Client; |
| 32 | + use lkaybob\phpFCMv1\Notification; |
| 33 | + use lkaybob\phpFCMv1\Recipient; |
| 34 | + ``` |
| 35 | + |
| 36 | +* Create Necessary class instances, Client, Recipient, Notification/Data |
| 37 | + |
| 38 | + ```php |
| 39 | + // Client instance should be created with path to service account key file |
| 40 | + $client = new Client('service_account.json'); |
| 41 | + $recipient = new Recipient(); |
| 42 | + // Either Notification or Data (or both) instance should be created |
| 43 | + $notification = new Notification(); |
| 44 | + ``` |
| 45 | + |
| 46 | +* Setup each instances with necessary information |
| 47 | + |
| 48 | + ```php |
| 49 | + // Recipient could accept individual device token, |
| 50 | + // the name of topic, and conditional statement |
| 51 | + $recipient -> setSingleREcipient('DEVICE_TOKEN'); |
| 52 | + // Setup Notificaition title and body |
| 53 | + $notification -> setNotification('NOTIFICATION_TITLE', 'NOTIFICATION_BODY'); |
| 54 | + // Build FCM request payload |
| 55 | + $client -> build($recipient, $notification); |
| 56 | + ``` |
| 57 | + |
| 58 | +* Fire in the FCM Server! |
| 59 | + |
| 60 | + ```php |
| 61 | + $result = $client -> fire(); |
| 62 | + // You can check the result |
| 63 | + // If successful, true will be returned |
| 64 | + // If not, error message will be returned |
| 65 | + echo $result; |
| 66 | + ``` |
| 67 | + |
| 68 | +### Further Example |
| 69 | + |
| 70 | +* Full Simple Example |
| 71 | + |
| 72 | + ```php |
| 73 | + <?php |
| 74 | + require_once __DIR__ . '/vendor/autoload.php'; |
| 75 | + |
| 76 | + use phpFCMv1\Client; |
| 77 | + use phpFCMv1\Notification; |
| 78 | + use phpFCMv1\Recipient; |
| 79 | + |
| 80 | + $client = new Client('service_account.json'); |
| 81 | + $recipient = new Recipient(); |
| 82 | + $notification = new Notification(); |
| 83 | + |
| 84 | + $recipient -> setSingleRecipient('DEVICE_TOKEN'); |
| 85 | + $notification -> setNotification('NOTIFICATION_TITILE', 'NOTIFICATION_BODY'); |
| 86 | + $client -> build($recipient, $notification); |
| 87 | + $client -> fire(); |
| 88 | + ``` |
| 89 | + |
| 90 | +* Using with *PRIOIRTY* option (for both Android & iOS) |
| 91 | + |
| 92 | + ```php |
| 93 | + <?php |
| 94 | + require_once __DIR__ . '/vendor/autoload.php'; |
| 95 | + |
| 96 | + use phpFCMv1\Client; |
| 97 | + use phpFCMv1\Config; |
| 98 | + use phpFCMv1\Notification; |
| 99 | + use phpFCMv1\Recipient; |
| 100 | + |
| 101 | + $client = new Client('service_account.json'); |
| 102 | + $recipient = new Recipient(); |
| 103 | + $notification = new Notification(); |
| 104 | + $config = new Config(); |
| 105 | + |
| 106 | + $recipient -> setSingleRecipient('DEVICE_TOKEN'); |
| 107 | + $notification -> setNotification('NOTIFICATION_TITLE', 'NOTIFICATION_BODY'); |
| 108 | + $config -> setPriority(Config::PRIORITY_HIGH); |
| 109 | + $client -> build($recipient, $notification, null, $config); |
| 110 | + $result = $client -> fire(); |
| 111 | + ``` |
| 112 | + |
| 113 | +* For independent platform (either Android or iOS) |
| 114 | + |
| 115 | + ``` |
| 116 | + // Option Instance for Android |
| 117 | + // Use phpFCMv1\AndroidConfig Class |
| 118 | + $androidConfig = new Config\AndroidConfig(); |
| 119 | + $androidConfig -> setPriority(Config\AndroidConfig::PRIORITY_HIGH); |
| 120 | + $client -> build($recipient, $notification, null, $androidConfig); |
| 121 | + |
| 122 | + // Option Instance for iOS (which is APNs header) |
| 123 | + // Use phpFCMv1\APNsCOnfig Class |
| 124 | + $apnsConfig = new APNsConfig(); |
| 125 | + $apnsConfig -> setPriority(APNsConfig::PRIORITY_HIGH); |
| 126 | + $client -> build($recipient, $notification, null, $apnsConfig); |
| 127 | + ``` |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +### Future Works |
| 132 | + |
| 133 | +[ ] Implement simultaneous send (Currently supports single recipient or topic one at a time) |
| 134 | + |
| 135 | +[ ] Setup Read the Docs |
| 136 | + |
| 137 | +[ ] Add CI Test |
| 138 | + |
| 139 | +[ ] Add CodeCov Badge |
0 commit comments