|
| 1 | +# PHP Firebase Cloud Messaging |
| 2 | +PHP API for Firebase Cloud Messaging from Google. |
| 3 | + |
| 4 | +Currently this app server library only supports sending Messages/Notifications via HTTP. |
| 5 | + |
| 6 | +See original Firebase docs: https://firebase.google.com/docs/ |
| 7 | + |
| 8 | +#Setup |
| 9 | +Install via Composer: |
| 10 | +``` |
| 11 | +composer require sngrl/php-firebase-cloud-messaging |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +Or add this to your composer.json and run "composer update": |
| 16 | + |
| 17 | +``` |
| 18 | +"require": { |
| 19 | + "sngrl/php-firebase-cloud-messaging": "dev-master" |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +#Send message to Device |
| 24 | +``` |
| 25 | +use sngrl\PhpFirebaseCloudMessaging\Client; |
| 26 | +use sngrl\PhpFirebaseCloudMessaging\Message; |
| 27 | +use sngrl\PhpFirebaseCloudMessaging\Recipient\Device; |
| 28 | +use sngrl\PhpFirebaseCloudMessaging\Notification; |
| 29 | +
|
| 30 | +$server_key = '_YOUR_SERVER_KEY_'; |
| 31 | +$client = new Client(); |
| 32 | +$client->setApiKey($server_key); |
| 33 | +$client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); |
| 34 | +
|
| 35 | +$message = new Message(); |
| 36 | +$message->addRecipient(new Device('_YOUR_DEVICE_TOKEN_')); |
| 37 | +$message |
| 38 | + ->setNotification(new Notification('some title', 'some body')) |
| 39 | + ->setData(['key' => 'value']) |
| 40 | +; |
| 41 | +
|
| 42 | +$response = $client->send($message); |
| 43 | +var_dump($response->getStatusCode()); |
| 44 | +``` |
| 45 | + |
| 46 | +#Send message to Topic |
| 47 | +Currently sending to topics only supports a single topic as recipient. Mutliple topic as outlined |
| 48 | +in the google docs don't seem to work, yet. |
| 49 | +``` |
| 50 | +use sngrl\PhpFirebaseCloudMessaging\Client; |
| 51 | +use sngrl\PhpFirebaseCloudMessaging\Message; |
| 52 | +use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic; |
| 53 | +use sngrl\PhpFirebaseCloudMessaging\Notification; |
| 54 | +
|
| 55 | +$server_key = '_YOUR_SERVER_KEY_'; |
| 56 | +$client = new Client(); |
| 57 | +$client->setserver_key($server_key); |
| 58 | +$client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); |
| 59 | +
|
| 60 | +$message = new Message(); |
| 61 | +$message->addRecipient(new Topic('_YOUR_TOPIC_')); |
| 62 | +$message |
| 63 | + ->setNotification(new Notification('some title', 'some body')) |
| 64 | + ->setData(['key' => 'value']) |
| 65 | +; |
| 66 | +
|
| 67 | +$response = $client->send($message); |
| 68 | +var_dump($response->getStatusCode()); |
| 69 | +``` |
0 commit comments