Firebase Push Notifications / Firebase Cloud Messaging (FCM): Push-Notifications per PHP versenden.

Auf dieser Seite halten wir kurz Scripts/Snippets fest, die wir während der Entwicklung eines Nachrichtensystems gebraucht haben. Ziel war es, Push-Notifications an ein Capacitor-App zu senden – allerdings mit einem PHP-Script als Backend. Da es in der offiziellen Doku von Firebase keine Beschreibung (mehr) für PHP gibt, sammeln wir die erforderlichen Scripte zum Nutzen der Firebase Cloud Messaging (FCM) über PHP auf dieser Seite.

Für Suchmaschinen: PHP FCM Push Notifications, Firebase Cloud Messaging PHP, Send Push Notification PHP FCM, Firebase PHP SDK, Firebase PHP Authentifizierung, Firebase Cloud Messaging API. OAUTH2 für PHP Google Push Notifications.

Push-Notifications per PHP – in der sehr „Basic“ Variante

<?php

// Pfad zum Credentials JSON.
// Kann hier erstellt werden: https://console.cloud.google.com/iam-admin/serviceaccounts/project
$credentialsFilePath = PATH_config . 'projektname-7da897df23.json';		

// Paket: composer require google/apiclient
$client = new \Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();

$googleAccessToken = $token['access_token'];
$apiurl = 'https://fcm.googleapis.com/v1/projects/projektname/messages:send';

$headers = [
	'Authorization: Bearer ' . $googleAccessToken,
	'Content-Type: application/json'
];

$notification_tray = [
	'title'             => "Test TRAY",
	'body'              => "Ein Test per PHP",
];

$in_app_module = [
	"title"          => "Test APP",
	"body"           => "Ein Test per PHP",
];

// @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?hl=de#Message
$message = [
	'message' => [
		'notification'     	=> $notification_tray,
		'data'             	=> $in_app_module,
		'token'			=> 'der...Token...ganz...langer...string....xxx',
	],
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));

$result = curl_exec($ch);
if ($result === FALSE) {
	//Failed
	die('Curl failed: ' . curl_error($ch));
}

curl_close($ch);

Firebase Cloud Messaging über PHP nutzen – die schönere Variante

<?php 

// https://github.com/kreait/firebase-php 

use \Kreait\Firebase\Factory; 
use \Kreait\Firebase\Http\HttpClientOptions; 
use \Kreait\Firebase\Messaging\CloudMessage; 
use \Kreait\Firebase\Messaging\Notification; 
use \Kreait\Firebase\Messaging\Topic; 

$absCredentialsFilePath = "/abs/path/to/json_from_google.json"; 
$options = HttpClientOptions::default(); 
$options = $options->withTimeOut( 3 );

$token = 'der...Token...ganz...langer...string....xxx';

$client = (new Factory)
	->withHttpClientOptions( $options )
	->withServiceAccount( $absCredentialsFilePath );

$messaging = $client->createMessaging();

$message = CloudMessage::withTarget( 'token', $token )
	->withNotification(Notification::create( 'Titel', 'Text' ));

$result = $messaging->send($message);