Skip to content

Instantly share code, notes, and snippets.

@qduc
Last active March 15, 2020 16:47
Show Gist options
  • Save qduc/a5616e96b4b381f7c85d3e31d31f133b to your computer and use it in GitHub Desktop.
Save qduc/a5616e96b4b381f7c85d3e31d31f133b to your computer and use it in GitHub Desktop.
Send message to Firebase Cloud Messaging
<?php
$client = new \Google_Client();
$client->useApplicationDefaultCredentials(); // Secret key path. See https://github.com/googleapis/google-api-php-client#authentication-with-service-accounts
$client->addScope(\Google_Service_FirebaseCloudMessaging::CLOUD_PLATFORM);
// Your Firebase project ID
$projectId = "--Insert your project id--";
$fcm = new \Google_Service_FirebaseCloudMessaging($client);
$request = new \Google_Service_FirebaseCloudMessaging_SendMessageRequest();
$message= new \Google_Service_FirebaseCloudMessaging_Message();
$notification = new \Google_Service_FirebaseCloudMessaging_Notification();
$notification->setTitle('Hello!');
$notification->setBody('From Firebase with love!');
$message->setToken($this->argument('token')); // Your client token. See https://firebase.google.com/docs/cloud-messaging/js/client
$message->setNotification($notification);
$request->setMessage($message);
try {
$response = $fcm->projects_messages->send("projects/$projectId", $request);
dump($response);
} catch (\Exception $exception) {
dump($exception);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment