-
-
Save Repox/64ac4b3582f8ac42a6a1b41667db7440 to your computer and use it in GitHub Desktop.
<?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]); |
Hello, Im using the service-account settings. However, I still get the error SENDER_ID_MISMATCH. Can someone help me?
The SENDER_ID_MISMATCH
error typically occurs when the Firebase Cloud Messaging (FCM) request is sent with a token that doesn't match the sender ID associated with the service account credentials being used. Here’s a step-by-step example of how to correctly set up and use the Google API PHP Client for Firebase Cloud Messaging Service v1, ensuring you avoid this error:
Key Points to Ensure
- Service Account JSON File: Make sure the path to the service account JSON file is correct.
- Firebase Project ID: Ensure that the project ID matches the one in your Firebase project settings.
- Device Token: Verify that the device token is valid and corresponds to the correct Firebase project.
Troubleshooting SENDER_ID_MISMATCH
- Token Validity: Ensure that the token you are using is from the same Firebase project associated with the service account credentials.
- Correct Project ID: Double-check that the
firebaseProjectId
in the script matches the project ID from the Firebase Console. - Scope and Permissions: Ensure that the service account has the necessary permissions to send FCM messages. The scope used in the script (
https://www.googleapis.com/auth/firebase.messaging
) should be correct.
If you've verified all these points and still encounter the issue, it's possible that the token has expired or is invalid. In such cases, try generating a new device token and test again.
@abhishekpwo , Key Points to Ensure::3
is my reason. Thank you.
Great! Welcome.
@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: