Created
April 12, 2018 06:04
-
-
Save Repox/64ac4b3582f8ac42a6a1b41667db7440 to your computer and use it in GitHub Desktop.
Google API PHP Client - Firebase Cloud Messaging Service v1 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This serves as an example of how to use the Google API PHP Client | |
* with Firebase Cloud Messaging Service. | |
* | |
* The client can be found here: | |
* https://github.com/google/google-api-php-client | |
* | |
* At the time of writing this, there's no Service object for the correct | |
* scope for Firebase Messaging, so here's an example of how this can be | |
* done with provididing the scope manually. | |
* | |
* Info regarding authorization and requests can be found here: | |
* https://firebase.google.com/docs/cloud-messaging/server | |
*/ | |
require 'vendor/autoload.php'; | |
$client = new Google_Client(); | |
// Authentication with the GOOGLE_APPLICATION_CREDENTIALS environment variable | |
$client->useApplicationDefaultCredentials(); | |
// Alternatively, provide the JSON authentication file directly. | |
$client->setAuthConfig(__DIR__.'/auth.json'); | |
// Add the scope as a string (multiple scopes can be provided as an array) | |
$client->addScope('https://www.googleapis.com/auth/firebase.messaging'); | |
// Returns an instance of GuzzleHttp\Client that authenticates with the Google API. | |
$httpClient = $client->authorize(); | |
// Your Firebase project ID | |
$project = "myproject-4e6ed"; | |
// Creates a notification for subscribers to the debug topic | |
$message = [ | |
"message" => [ | |
"topic" => "debug", | |
"notification" => [ | |
"body" => "This is an FCM notification message!", | |
"title" => "FCM Message", | |
] | |
] | |
]; | |
// Send the Push Notification - use $response to inspect success or errors | |
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]); |
@Repox ,
I used Composer to install locally, but with PHP 8.2
My shared hosting is PHP 7.4, I can not migrate at this time, because I encounteer an error 500.
How is it possible to download locally firebase-php with PHP 7.4 ?
I install a virtual machine with PHP 7.4 locally to obtain firebase-php 6.9.6 and now:
composer require "kreait/firebase-php:6.9.6"
PHP Fatal error: Uncaught Error: Call to undefined function Symfony\Component\Console\str_contains() in /usr/share/php/Symfony/Component/Console/Application.php:860
Stack trace:
#0 /usr/share/php/Symfony/Component/Console/Application.php(840): Symfony\Component\Console\Application->doRenderThrowable()
#1 /usr/share/php/Symfony/Component/Console/Application.php(154): Symfony\Component\Console\Application->renderThrowable()
#2 [internal function]: Symfony\Component\Console\Application->Symfony\Component\Console\{closure}()
#3 {main}
thrown in /usr/share/php/Symfony/Component/Console/Application.php on line 860
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Repox ,
So sorry, but I can not understand where scripts firebase-php are included, with use ? or namespace or other ?
May I declarde a variable in shared hosting ?
I am using v6.9.6 with php 7.4 on my shared hosting.
Uncaught Error: Class 'Kreait\Firebase\Factory' not found in //testsnotifs.php:16 Stack trace: #0 //testsnotifs.php(91): notifPushEnvoi_FirebaseAPI('d7-so8quEvo:APA...', 'Alors...', 'Goooooo', 1) #1 {main} thrown in /****/testsnotifs.php on line 16
Ok, so sorry, I understood why using composer.
Many thanks.